繁体   English   中英

"我的 Python 代码中的错误:Python 中的 elseif 错误"

[英]Error in my Python code: elseif error in Python

下面的代码不起作用

N = int(input())
print("Enter number",N)
 if(N%2!=0):
 print("Weird")
  elif (N%2==0) and (N>=2 and N<=5):
      print("Not Weird")
      elif(N%2==0) and (N>=6 and N<=20):
          print("Weird")
         else:
         print("Not Weird")

我收到以下错误:

File "solution.py", line 7
elif (N%2==0) and (N>=2 and N<=5):
   ^
SyntaxError: invalid syntax

问题在于缩进。

N = int(input())
print("Enter number",N)
if(N%2!=0):
   print("Weird")
elif (N%2==0) and (N>=2 and N<=5):
   print("Not Weird")
elif(N%2==0) and (N>=6 and N<=20):
   print("Weird")
else:
   print("Not Weird")

根据您的缩进,您不能在没有if情况下启动elif ,您应该始终启动if然后elif 您可以检查条件如下:

print("Enter number")
N = int(input())
if(N%2!=0):
    print("Weird")
elif (N%2==0) and (N>=2 and N<=5):
    print("Not Weird")
elif(N%2==0) and (N>=6 and N<=20):
    print("Weird")
else:
     print("Not Weird")

对于您的编码约定,您可以随时查看PEP 8 -- Python 代码风格指南

我的代码不起作用。 请帮忙!

if __name__ == '__main__':
    n = int(raw_input().strip())
if n%2! =0:
print("Weird")
else:
if n>=2 and n<=5:
print("Not Weird")
elif n>=6 and n<=20:
print("Weird")
elif n>20:
print("Not Weird")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM