Python意外缩进

时间:2015-04-20 15:57:35

标签: python syntax-error

错误:

Traceback (most recent call last):
File "python", line 2
if Number == "50" and "51" and "52" and "53" and "54" and "55" and "56 "and "57" and "58" and "59" and "60":
 ^
IndentationError: unexpected indent

代码:

Number = input ("Please enter a number between 50 and 60: ")
    if Number == "50" and "51" and "52" and "53" and "54" and "55" and "56 "and "57" and "58" and "59" and "60":
print ("The number ") + (Number) + (" is with in range")

我正在尝试运行此python代码,但我不断收到错误消息“意外缩进”。我不确定什么是错的。间距似乎很好。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

你的缩进是倒退的。 if之后的行应该缩进。

Number = input ("Please enter a number between 50 and 60: ")
if Number == "50" and "51" and "52" and "53" and "54" and "55" and "56 "and "57" and "58" and "59" and "60":
    print ("The number ") + (Number) + (" is with in range")

答案 1 :(得分:2)

您是否在if之后用四个空格缩进了这一行?请注意您的代码中存在逻辑缺陷;你的if陈述永远不会成真。您的意思是使用or吗?在这种情况下,您可以使用此代码段更有效地实现它:

Number = input ("Please enter a number between 50 and 60: ")
if 50 <= Number <= 60:
    print ("The number ") + (Number) + (" is with in range")

答案 2 :(得分:0)

您的if语句缩进,print没有缩进。这应该 反过来说。此外,正如之前的人所说,你的if声明 不正确,这将产生另一个错误。:

Number = input("Please enter a number between 50 and 60: ")
if Number in ['50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60']:
    print("The number %s is within range" % Number)
但是,我不确定你为什么要将数字视为一个字符串。我这样做:

try:
    Number = int(input("Please enter a number between 50 and 60: "))
except ValueError:
    print("You were supposed to enter a number!")
else:
    if 50 <= Number <= 60:
        print("The number %d is within range" % Number)