# What is the error in this coding?

num=int(input("Enter a number:"))
if(num==0):
fact=1
fact=1
for i in range(1,num1+):
fact=fact*i
print("Factorial of",num,"is",fact)

+1 vote
answered Sep 9 by (5,120 points)
• Logical error in 'if statement' since the code would only execute further if the number entered is 0. We would want a number 1 or greater (only if one if statement is present). In your case, you would want if(num != 0)
• Double declaration and assignment of fact variable (not causes any issues but not needed)
• In your for loop, 1 and + are wrong way round. We would want for..... range(1, num+1)
• Alternative method of fact=fact*i is fact*=i
• However, with the fixes, the factorial statement is printed multiple times and only the last statement printed is correct. To only print once, we would move the print statement to indented inline with for.

A working solution but slightly different to support negative numbers, 0 and 1 or greater:

num = int(input("Enter a number: "))

factorial = 1

if num < 0:
print("Negative number don't have a factorial")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

answered Sep 19 by (180 points) 2 flags
• Logical error in 'if statement' since the code would only execute further if the number entered is 0. We would want a number 1 or greater (only if one if statement is present). In your case, you would want if(num != 0)
• Double declaration and assignment of fact variable (not causes any issues but not needed)
• In your for loop, 1 and + are wrong way round. We would want for..... range(1, num+1)
• Alternative method of fact=fact*i is fact*=i
• However, with the fixes, the factorial statement is printed multiple times and only the last statement printed is correct. To only print once, we would move the print statement to indented inline with for.

A working solution but slightly different to support negative numbers, 0 and 1 or greater:

num = int(input("Enter a number: "))

factorial = 1

if num < 0:
print("Negative number don't have a factorial")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

+1 vote
answered Sep 25 by (520 points)
Hello.

number = int(input("Enter number: "))
f = number
factorial_value = 1
while number > 0:
factorial_value *= number
number -= 1
print("{}! = {}".format(f, factorial_value))

#f variable is used to format the ans value
#because in loop, the last value of number will be 0
#and 0! is 0 and not the ans that we get.
#factorial_value is set to 1 because if set to 0
#ans will always be 0.
#In the loop, number is multiplied with factorial value
#then number is decreased by 1
#and the loop will continue till number = 0 and
#as 0 = 0 and not greater than 0
#loop breaks and program prints the factorial value
+1 vote
answered Sep 26 by (160 points)
for i in range(1,num1+):
^
SyntaxError: invalid syntax

in for after num1 + should not come
–1 vote
answered Sep 28 by (540 points)

I don't do python really, but it looks like this code is incorrect.

answered Oct 16 by (200 points)

I think there are few errors in your code.

You just need to understand and correct few errors in your code.

num=int(input("Enter a number:"))
fact=1
if(num==0):
fact=1
else:
for i in range(1,num+1):
fact=fact*i
print("Factorial of",num,"is",fact)

1.Before if statement, you have to declare a fact.

2.It is better to use if-else statement.
3.In for Statement,The range should be num+1.because the ending index is not considered in the loop.