I am supposed to filter the ages of the users through the following rule: if users >=120 cm then print("You can ride the rollercoaster!")
, if users <=119 cm then print ("Sorry, you must grow taller, see you next time.")
Everything is going ok with the code when users are <=119, output:
> Welcome to the rollercoaster! Enter your height in cm, please: 52
> Sorry, you must grow taller, see you next time.
But when users are >=120, this happens:
> 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.
As you can see, the last output line it should not be happening.
Is there some way to break
the if statement? I've tried the break
function and I got a:
SyntaxError: 'break' outside loop
This is the whole code:
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 $5. Enjoy!")
elif age <= 18:
print("Please pay $7. Enjoy!")
elif age >= 18 and age <= 44:
print("Please pay $12. 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 $9. 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 $5. Enjoy!")
elif age <= 18:
print("Please pay $7. Enjoy!")
elif age >= 18 and age <= 44:
print("Please pay $12. 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 $9. 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.")
CodePudding user response:
You missed an elif statement in the main condition.
Try this:
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 $5. Enjoy!")
elif age <= 18:
print("Please pay $7. Enjoy!")
elif age >= 18 and age <= 44:
print("Please pay $12. 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 $9. 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 $5. Enjoy!")
elif age <= 18:
print("Please pay $7. Enjoy!")
elif age >= 18 and age <= 44:
print("Please pay $12. 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 $9. 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.")
See the difference?