Recently, I use pycharm to practice typing some codes, but I encounter with some problems, the factorial of 5 should be output. But the output is only 5. Could you please take some time to help me check the errors? I will appreciate for that!
The code as follows:
def factorial(n):
r = 1
while n > 1:
r *= n
n -= 1
return r
I’m just new to Mosh’s tutorials on YouTube, so I haven’t yet gotten to the functions lesson. But I used a for loop to solve your problem. Check out the solution below
x = int(input("Please enter a number: "))
numbers = []
for item in range(1,x+1):
numbers.append(item)
numbers.reverse()
factorial = 1
for item in numbers:
factorial = factorial * item
print(f"The factorial of the number you entered is {factorial}")