Question 2
Level 1
Question: Write a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program: 8 Then, the output should be: 40320
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
Solution:
#!/usr/bin/env python # encoding: utf-8 ''' @author: @contact: @software: @file: q2.py @time: 2021/1/5 9:27 上午 @desc: '''
def fact(num): if num == 0: return 1 else: return fact(num-1)*num
test_num = int(input("Calculate the factorial of:")) print("The factorial of test_num is", fact(test_num))