简体   繁体   中英

Getting "SyntaxError: invalid syntax" when using elif

I'm getting the following error when I run the following program

File "main.py", line 14
    elif age >= 45 and <= 55:
                       ^
SyntaxError: invalid syntax

This is the code that I wrote

print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))


if height >= 120:
  print("You can ride the rollercoaster!")
  age = int(input("What is your age? "))
  if age < 12:
      bill = 5
      print("Child tickets are $5.")
  elif age <= 18:
      bill = 7
      print("Youth tickets are $7.")
  elif age >= 45 and <= 55:
      bill = 0
      print("Everything is going to be okay! Have a free ride on us <3.")

I was under the impression that this line was written properly

elif age >= 45 and <= 55:

You can use brackets to ensure that there are two statements in one line and they do not conflict with them. Here is my answer. you just need to modify the line or you can copy paste it.

print("Welcome to the rollercoaster!")
    height = int(input("What is your height in cm? "))
    
    
    if height >= 120:
      print("You can ride the rollercoaster!")
      age = int(input("What is your age? "))
      if age < 12:
          bill = 5
          print("Child tickets are $5.")
      elif age <= 18:
          bill = 7
          print("Youth tickets are $7.")
      elif (age >= 45) and (age <= 55):
          bill = 0
          print("Everything is going to be okay! Have a free ride on us <3.")

Just edit your elif statement from

elif age >=45 and <=55

To

elif age>=45 and age<=55

You should write the conditions completely

In python you can say:

elif 45 >= age <= 55: ... etc...

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