Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序中的缩进错误_Python_Indentation - Fatal编程技术网

Python程序中的缩进错误

Python程序中的缩进错误,python,indentation,Python,Indentation,我的代码是- 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) 第行出现缩进错误: number = number-

我的代码是-

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)
第行出现缩进错误:

number = number- 1
我的错误是:
意外缩进

为什么呢

问候,


Nupur

您发布的代码完全没有缩进。记住,对于Python

缩进代码后,您仍然面临两个错误:

  • 您正在使用
    input
    ,这意味着您正在使用Python3,或者您应该使用
    raw\u input
    。看,这很好地解释了差异。如果您使用的是Python3,那么使用
    print
    语句是不正确的:您应该使用。如果您使用的是Python2,那么应该使用
    raw\u input
  • 您的函数,
    factorial
    ,需要一个数字,但您要传递一个字符串(这是
    raw\u input
    input
    的返回值)。首先转换为int
  • 这段代码是正确缩进的,适用于Python3。如果您正在使用Python2,请使用
    raw\u input
    而不是
    input

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

    您需要将输入从str转换为int。

    讽刺的是,您去掉了所有缩进。准确地复制它以便调查。你能用你正在使用的实际格式发布你的代码吗?你肯定知道,在Python缩进方面。不要忘记空格和制表符也不能混合。
    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))