Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rollercoaster Project

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading