Answer:
Here is the complete code to complete factorial_str()'s recursive case:
Just add this line to the recursive part of the code for the solution:
output_string += factorial_str(next_counter,next_value)
The above statement calls factorial_str() method recursively by passing the values of next_counter and next_value. This statement continues to execute and calls the factorial_str() recursively until the base case is reached.
Explanation:
Here is the complete code:
def factorial_str(fact_counter, fact_value): #method to find the factorial
output_string = '' #to store the output (factorial of an input number)
if fact_counter == 0: # base case 1 i.e. 0! = 1
output_string += '1' # displays 1 in the output
elif fact_counter == 1: #base case 2 i.e. 1! = 1
output_string += str(fact_counter) + ' = ' + str(fact_value) #output is 1
else: #recursive case
output_string += str(fact_counter) + ' * ' #adds 8 between each value of fact_counter
next_counter = fact_counter - 1 #decrement value of fact_counter by 1
next_value = next_counter * fact_value #multiplies each value of fact_value by next_counter value to compute the factorial
output_string += factorial_str(next_counter,next_value) #recursive call to factorial_str to compute the factorial of a number
return output_string #returns factorial
user_val = int(input()) #takes input number from user
print('{}! = '.format(user_val),end="") #prints factorial in specified format
print(factorial_str(user_val,user_val)) #calls method by passing user_val to compute the factorial of user_val
I will explain the program logic with the help of an example:
Lets say user_val = 5
This is passed to the method factorial_str()
factorial_str(fact_counter, fact_value) becomes:
factorial_str(5, 5):
factorial_str() method has two base conditions which do not hold because the fact_counter is 5 here which is neither 1 nor 0 so the program control moves to the recursive part.
output_string += str(fact_counter) + ' * ' adds an asterisk after the value of fact_counter i.e. 5 as:
5 *
next_counter = fact_counter - 1 statement decrements the value of fact_counter by 1 and stores that value in next_counter. So
next_counter = 5 - 1
next_counter = 4
next_value = next_counter * fact_value multiplies the value of next_counter by fact_value and stores result in next_value. So
next_value = 4 * 5
next_value = 20
output_string += factorial_str(next_counter,next_value) this statement calls the factorial_str() to perform the above steps again until the base condition is reached. This statement becomes:
output_string = output_string + factorial_str(next_counter,next_value)
output_string = 5 * 4 = 20
output_string = 20
Now factorial_str(next_counter,next_value) becomes:
factorial_str(4,20)
output_string += str(fact_counter) + ' * ' becomes
5 * 4 * 3
next_counter = fact_counter - 1 becomes:
4 - 1 = 3
next_counter = 3
next_value = next_counter * fact_value becomes:
3 * 20 = 60
next_value = 60
output_string = 5 * 4 * 3= 60
output_string = 60
factorial_str(next_counter,next_value) becomes:
factorial_str(3,60)
output_string += str(fact_counter) + ' * ' becomes
5 * 4 * 3 * 2
next_counter = fact_counter - 1 becomes:
3 - 1 = 2
next_counter = 2
next_value = next_counter * fact_value becomes:
2 * 60 = 120
next_value = 120
output_string += factorial_str(next_counter,next_value) becomes:
output_string = 120 + factorial_str(next_counter,next_value)
output_string = 5 * 4 * 3 * 2 = 120
factorial_str(2,120)
output_string += str(fact_counter) + ' * ' becomes
5 * 4 * 3 * 2 * 1
next_counter = fact_counter - 1 becomes:
2 - 1 = 1
next_counter = 1
next_value = next_counter * fact_value becomes:
1 * 120 = 120
next_value = 120
output_string += factorial_str(next_counter,next_value) becomes:
output_string = 120 + factorial_str(next_counter,next_value)
factorial_str(next_counter,next_value) becomes:
factorial_str(1, 120)
Now the base case 2 evaluates to true because next_counter is 1
elif fact_counter == 1
So the elif part executes which has the following statement:
output_string += str(fact_counter) + ' = ' + str(fact_value)
output_string = 5 * 4 * 3 * 2 * 1 = 120
So the output of the above program with user_val = 5 is:
5! = 5 * 4 * 3 * 2 * 1 = 120