过山车项目

Rollercoaster Project

我应该通过以下规则过滤用户的年龄:如果用户 >=120 厘米,则 print("You can ride the rollercoaster!"),如果用户 <=119 厘米,则 print ("Sorry, you must grow taller, see you next time.")

当用户 <=119 时,代码一切正常,输出:

> Welcome to the rollercoaster! Enter your height in cm, please: 52
> Sorry, you must grow taller, see you next time.

但是当用户 >=120 时,会发生这种情况:

> Welcome to the rollercoaster! Enter your height in cm, please: 178 You
> can ride the rollercoaster! Enter your age, please: 80 You are more
> than 70 years old, you won a free ride, enjoy.! 
> Sorry, you must grow taller, see you next time.

如您所见,最后一行输出不应该发生。 有什么方法可以 break if 语句吗?我已经尝试了 break 函数,我得到了一个:

SyntaxError: 'break' outside loop

这是完整的代码:

print("Welcome to the rollercoaster!")
height = int(input("Enter your height in cm, please: "))
if height >= 120:
  print("You can ride the rollercoaster!")
  age = int(input("Enter your age, please: "))
  if age < 12:
    print ("Please pay . Enjoy!")
  elif age <= 18:
    print("Please pay . Enjoy!")
  elif age >= 18 and age <= 44:
    print("Please pay . Enjoy!")
  elif age >= 45 and age <=55:
    print ("Everything is going to be ok. Have a free ride on us!")
  elif age <= 69:
   print ("Please pay . Enjoy!")
  else:
    print (f"You are more than 70 years old, you won a free ride, enjoy.!")
if height >= 300:
  print ("For security reasons, you are reasigned to the next rollercoaster.")
  age = int(input("Enter your age, please: "))
  if age < 12:
    print ("Please pay . Enjoy!")
  elif age <= 18:
    print("Please pay . Enjoy!")
  elif age >= 18 and age <= 44:
    print("Please pay . Enjoy!")
  elif age >= 45 and age <=55:
    print ("Everything is going to be ok. Have a free ride on us!")
  elif age <= 69:
   print ("Please pay . Enjoy!")
  else:
    print (f"You are more than 70 years old, you won a free ride, enjoy.!")
else:
  print ("Sorry, you must grow taller, see you next time.")

您错过了主要条件中的 elif 语句。

试试这个:

print("Welcome to the rollercoaster!")
height = int(input("Enter your height in cm, please: "))
if height >= 120 and height <300:
  print("You can ride the rollercoaster!")
  age = int(input("Enter your age, please: "))
  if age < 12:
    print ("Please pay . Enjoy!")
  elif age <= 18:
    print("Please pay . Enjoy!")
  elif age >= 18 and age <= 44:
    print("Please pay . Enjoy!")
  elif age >= 45 and age <=55:
    print ("Everything is going to be ok. Have a free ride on us!")
  elif age <= 69:
   print ("Please pay . Enjoy!")
  else:
    print (f"You are more than 70 years old, you won a free ride, enjoy.!")
elif height >= 300:
  print ("For security reasons, you are reasigned to the next rollercoaster.")
  age = int(input("Enter your age, please: "))
  if age < 12:
    print ("Please pay . Enjoy!")
  elif age <= 18:
    print("Please pay . Enjoy!")
  elif age >= 18 and age <= 44:
    print("Please pay . Enjoy!")
  elif age >= 45 and age <=55:
    print ("Everything is going to be ok. Have a free ride on us!")
  elif age <= 69:
   print ("Please pay . Enjoy!")
  else:
    print (f"You are more than 70 years old, you won a free ride, enjoy.!")
else:
  print ("Sorry, you must grow taller, see you next time.")

看出区别了吗?