Can you help me to check the program code error?

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

print(factorial(5))

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 :slight_smile:

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}")

Does this help?

try this:

import math

print(math.factorial(5))

**you need to import the math module first and you dont need all those codes at all.

I know the reason, your advice is really helpful, thank you all!