[英]Error in my Python code: elseif error in Python
below code is not working下面的代码不起作用
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")
I am getting below error:我收到以下错误:
File "solution.py", line 7
elif (N%2==0) and (N>=2 and N<=5):
^
SyntaxError: invalid syntax
Issue is with indentation.问题在于缩进。
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")
Based on your indentation, you cannot start elif
without if
, you should always start if
and then elif
.根据您的缩进,您不能在没有
if
情况下启动elif
,您应该始终启动if
然后elif
。 you can check for condition as below:您可以检查条件如下:
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")
For your coding conventions, you can always look on to PEP 8 -- Style Guide for Python Code对于您的编码约定,您可以随时查看PEP 8 -- Python 代码风格指南
my code is not working.我的代码不起作用。 please help!
请帮忙!
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.