My code is -
def factorial(number):
result = 1
while number > 0:
result = result*number
number = number- 1
return result
in_variable = input("Enter a number to calculate the factorial of")
print factorial(in_variable)
I am getting an indentation error on line :
number = number- 1
My error is: Unexpected indent
Why is that?
Regards,
Nupur
-
1Ironically, you stripped out all the indentation. Reproduce it accurately so that it can be investigated.TigerhawkT3– TigerhawkT32015年04月16日 22:59:07 +00:00Commented Apr 16, 2015 at 22:59
-
Can you post your code with the actual formatting you're using? As you're no doubt aware, in Python indentation matters. Don't forget that spaces and tabs cannot be mixed either.Oliver W.– Oliver W.2015年04月16日 22:59:40 +00:00Commented Apr 16, 2015 at 22:59
3 Answers 3
The code you've posted shows no indentation at all. Remember, for Python, indentation matters.
After indenting your code, you are still facing two bugs:
- you are using
input, which means you are using Python3 OR you should have been usingraw_input. See this discussion, which explains the difference very well. If you are using Python3, then it's not correct to use theprintstatement: you should be using theprintfunction. If you're using Python2, you should be usingraw_input. - Your function,
factorial, expects a number, yet you are passing it a string (which is the return value ofraw_inputandinput). Convert to int first.
This code is properly indented and works on Python3. Use raw_input rather than input if you're working with Python2.
def factorial(number):
result = 1
while number > 0:
result = result*number
number = number- 1
return result
in_variable = input("Enter a number to calculate the factorial of")
print(factorial(int(in_variable)))
Comments
def factorial(number):
result = 1
while number > 0:
result = result * number
number = number- 1
return result
in_variable = int(input("Enter a number to calculate the factorial of"))
print(factorial(in_variable))
You need to convert the input from str to int.
Comments
If you are not sure your indention str of int. You can easily check it with type(in_variable) and if it is str, you should change it to int with in_variable = int(input("Enter a number to calculate the factorial of"))
def factorial(number):
result = 1
while number > 0:
result = result*number
number = number- 1
return result
in_variable = input("Enter a number to calculate the factorial of")