Table of Contents
Python Exercise 2 Problem
In this Python exercise, write a Python program that will compute the factorial of a given number. The given number is entered with the user input between 1 and 10. Also, the result of this Python program should result with a print on a single line.
Note: This exercise is completed in Python 3.7
Python Exercise 2 Solution
Python Code Input:
num = int(input("\nEnter a number between 1 and 10: ")) try: x = int(num) print(f"\nThe number entered is {x}.") except ValueError: print(f"\nIn order to convert a number to an integer, the value entered must be a number. You entered {num} .") def fact(x): if x == 0: return 1 return x * fact(x - 1) print(f"\nThe result of {x} * fact({x} - 1) is", (fact(x)))
User Input:
Enter a number between 1 and 10: 5
Python Code Output:
The number entered is 5. The result of 5 * fact(5 - 1) is 120