elif语句获取无效语法错误。如何克服这个?

时间:2019-08-17 16:00:02

标签: python python-3.x if-statement

我正在尝试运行此代码,但是遇到了错误,并花了所有时间进行调试。寻求帮助

n = int(input())
if n % 2 == 1: 
 print("Weird")
elif(n%2==0) and 2<=n<=5:      
     print("Not Weird")
elif(n%2==0) and 6<=n<=20:
         print("Weird")
else:
             print(" Not Weird")

2 个答案:

答案 0 :(得分:3)

if/elif/else语句必须全部使用相同的缩进级别。

if x:
    # do stuff
elif y:
    # do other stuff
elif z:
    # do more stuff
else:
    # do something else

答案 1 :(得分:0)

您不应缩进elif等。它们应该像缩进一样。另外,您无需将条件语句括在括号中。这有效:

n = int(input())
if n%2 == 1:
 print("Weird")
elif n%2 == 0:      
    print("Not Weird")
elif n%2 == 0 and 6 <= n <= 20:
    print("Weird")
else:
    print(" Not Weird")