简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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