Write code to complete print_factorial()\'s recursive case. Sample output if use
ID: 3828336 • Letter: W
Question
Write code to complete print_factorial()'s recursive case. Sample output if user_val is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 def print_factorial(fact_counter, fact_value): output_string = ' ' if fact_counter == 0: #Base case: theta! = 1 output string += '1' elif fact_counter == 1: # Base case: print 1 and result output_string += str(fact_counter) + ' = ' + str(fact_value) else: #Recursive case output_string += str (fact_counter) + " * " next_counter = fact_counter - 1 next_value = next_counter * fact_value output_string += '''Your solution goes here''' return output_string user_val = 5 print("%d! = ' % user_val, end="'") print(print_factorial(user_val, user_val))Explanation / Answer
def print_factorial(fact_counter, fact_value, output_string):
output_string=""
if fact_counter==0:
output_string+='1'
elif fact_counter==1:
output_string+=str(fact_counter)+ ' = ' + str(fact_value)
else:
output_string+=str(fact_counter)+ ' * '
fact_value=(fact_counter-1)*fact_value;
output_string+=print_factorial(fact_counter-1,fact_value,output_string)
return output_string
print(print_factorial(5,5,""))
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.