Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write code to complete print_factorial()\'s recursive case. Sample output if use

ID: 3932740 • Letter: W

Question

Write code to complete print_factorial()'s recursive case. Sample output if user_val is 5:

def print_factorial(fact_counter, fact_value):
    output_string = ''

    if fact_counter == 0:      # Base case: 0! = 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 = ''
if fact_counter == 0: # Base case: 0! = 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 += print_factorial(fact_counter-1, next_value)
return output_string
user_val = 5
print('%d! = ' % user_val, end="")
print(print_factorial(user_val, user_val))

Output:

sh-4.3$ python main.py 5! = 5 * 4 * 3 * 2 * 1 = 120

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote