0

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

Adam Spiers
18.2k5 gold badges49 silver badges68 bronze badges
asked Apr 16, 2015 at 22:57
2
  • 1
    Ironically, you stripped out all the indentation. Reproduce it accurately so that it can be investigated. Commented 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. Commented Apr 16, 2015 at 22:59

3 Answers 3

2

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:

  1. you are using input, which means you are using Python3 OR you should have been using raw_input. See this discussion, which explains the difference very well. If you are using Python3, then it's not correct to use the print statement: you should be using the print function. If you're using Python2, you should be using raw_input.
  2. Your function, factorial, expects a number, yet you are passing it a string (which is the return value of raw_input and input). 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)))
answered Apr 16, 2015 at 23:06
Sign up to request clarification or add additional context in comments.

Comments

0
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.

answered Apr 16, 2015 at 23:11

Comments

0

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

answered Jul 31, 2022 at 19:07

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.