Rollercoaster Project - python

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

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?

Related

How to let the user to input string as well as integers in my program?

I have a question. how can I let the user to enter 'quit' (A string) in my program as well as integers? Thank you
message = "Please enter your age.\nEnter quit to exit the program.\n"
age = ""
while age != 'quit':
age = input(messages)
if age == 'quit':
break
age = int(age)
elif age < 3:
print("The ticket you purchased is free.")
elif age >= 3 and age < 13:
print("The ticket you purchased cost $10.")
elif age >= 13:
print("The ticket you purchased cost $15.")
Your code is close to working, but there are two issues:
message = "Please enter your age.\nEnter quit to exit the program.\n"
age = ""
while age != 'quit':
age = input(message) # variable is called message
if age == 'quit':
break
age = int(age)
if age < 3: # start a new if/elif/else block
print("The ticket you purchased is free.")
elif age >= 3 and age < 13:
print("The ticket you purchased cost $10.")
elif age >= 13:
print("The ticket you purchased cost $15.")
I'd also simplify the if/elif/else block to
if age < 3:
print("The ticket you purchased is free.")
elif age < 13: # no need for >= 3 check since that is done above
print("The ticket you purchased cost $10.")
else: # no need for final check since all special cases done above
print("The ticket you purchased cost $15.")

How do I ask for an input from a user and check that value within a range to give me the correct response

So my professor for python assigned an exercise which was to create a program to accept a user input or "score" and create an if else loop with an if else nested loop inside the first loop to check if the score is greater than or equal to 80. If that's satisfied, print a statement saying "you passed." and if the score is equal to 90 or more, then it should say"you got an A." and if the score is below an 80, it should say "you failed."
so far, this is the code I was able to come up with. And it runs up until the score entered is anything equal to or more than 90.
userScore = int(input('enter score:\n'))
if userScore < 80:
userScore = int(input('you failed. Try again:\n'))
if userScore == 80 or userScore > 80:
print(f'good job, you got a B.')
elif userScore >= 90:
print('you got an A')
else:
print('enter a differnt score')
as per your question your code should be :
score = int(input("Please enter your score: "))
if score >= 80:
print("You passed.")
if score >= 90:
print("You got an A.")
else:
print("You failed.")
while(1):
score = int(input("Please enter your score: "))
if score >= 80:
print("You passed.")
if score >= 90:
print("You got an A.")
break
else:
print("You failed.")
while(1):
score = int(input("Please enter your score pal: "))
if score >= 80:
print("You passed the exam.")
if score >= 90:
print("You got an A in the exam.")
break
else:
print("You failed the exam.")

Why the While loop keep running in the following Python code?

I am new to Python, and could not figure out why the While loop is kept running in the following code?
prompt = "Please enter the customer's age: "
age = input(prompt)
while age != 'quit':
age = int(age)
if age <= 3:
print("Welcome little buddy, no charges for you!")
elif age > 3 and age < 12:
print("The ticket price is $10")
elif age >= 12:
print("The ticket price is 15")
elif age == 'quit':
print("rerun the code")
i think you expect this from your code..i did some changes.your while loop didn't stop because it didn't have a reason to stop.it is testing same value again and again against condition
def agetest():
age = input("Please enter the customer's age: ")
age = int(age)
if 0<age <= 3:
print("Welcome little buddy, no charges for you!")
agetest()
elif age > 3 and age < 12:
print("The ticket price is $10")
agetest()
elif age >= 12:
print("The ticket price is 15")
agetest()
elif age == -1:
print("thnks")
agetest()
here is the answer with while loop
age=0
while(age!= -1):
print("Please enter the customer's age: ")
age = int(input())
if 0<age <= 3:
print("Welcome little buddy, no charges for you!")
elif age > 3 and age < 12:
print("The ticket price is $10")
elif age >= 12:
print("The ticket price is 15")
elif age == -1:
print("thanks")
I have taken your code and did some modifications.
problems were :
you were getting input out the loop so it was not interactive.
there was no statement to modify age to break the loop.
you can not cast the input as integer if you are expecting a string input.
while True:
prompt = "Please enter the customer's age: "
age = input(prompt)
if age == 'quit':
print("rerun the code")
break
elif age <= 3:
print("Welcome little buddy, no charges for you!")
elif age > 3 and age < 12:
print("The ticket price is $10")
elif age >= 12:
print("The ticket price is 15")

python code running else statement even after if statement is true

I am learning python programming and was going through If Else conditions. Even if the If statement is true, my code executes else condition.
Please check the code below:
age = int(input("Enter your Age (in years)"))
sex = input("Enter you Sex(M/F)")
if(sex == 'M'):
if(age < 20):
print("You are just a teen.")
if(age >= 20 and age < 25):
print("You are a young man now.")
elif(age >=25 and age < 30):
print("You are a mature man now")
else:
print("You are getting old")
if(sex == 'F'):
if(age < 20):
print("You are just a teen.")
if(age >= 20 and age < 25):
print("You are a young woman now.")
elif(age >=25 and age < 30):
print("You are a lady now")
Over here, if i enter the age as 2 and sex as M, the code goes in first condition and prints the message
"You are just a team"
Along with it, the code also run the else condition and prints
You are getting old
I don't understand this behaviour. I checked for indentation and all indentations are correct.
You accidentally made it a double if which would cause both statements to execute.
age = int(input("Enter your Age (in years)"))
sex = input("Enter you Sex(M/F)")
if(sex == 'M'):
if(age < 20):
print("You are just a teen.")
elif(age >= 20 and age < 25): # notice now it is one if-elif block
print("You are a young man now.")
elif(age >=25 and age < 30):
print("You are a mature man now")
else:
print("You are getting old")
if(sex == 'F'):
if(age < 20):
print("You are just a teen.")
elif(age >= 20 and age < 25): # same here
print("You are a young woman now.")
elif(age >=25 and age < 30):
print("You are a lady now")
Switch
if(age < 20):
print("You are just a teen.")
if(age >= 20 and age < 25):
print("You are a young man now.")
with
if(age < 20):
print("You are just a teen.")
elif(age >= 20 and age < 25):
print("You are a young man now.")
What's happening is that your second if statement inside of if sex == 'M' isn't getting fulfilled because the age is not between 20 and 25. Since the elif isn't fulfilled either, whats inside the else block runs.
In the code snippet you've given, the else is connected to the second if statement: if(age >= 20 and age < 25):. The first "if" executes fine, but then when the second "if" fails it executes the "else". This can be fixed by changing the second "if" to an "elif":
if(sex == 'M'):
if(age < 20):
print("You are just a teen.")
elif(age >= 20 and age < 25):
print("You are a young man now.")
elif(age >=25 and age < 30):
print("You are a mature man now")
else:
print("You are getting old")
It is printing correct output. First, it is checking that age is less than 20 years, which is correct, it then print "You are just a teen.".
if(sex == 'M'):
if(age < 20):
print("You are just a teen.")
After that it checks second 'if' statement, then 'elif' and then it goes to 'else' and print that statement as there was no match previously.
if(age >= 20 and age < 25):
print("You are a young man now.")
elif(age >=25 and age < 30):
print("You are a mature man now")
else:
print("You are getting old")
You may have made a typo here:
if(age >= 20 and age < 25):
print("You are a young man now.")
Possibly, you are trying to use 'if' instead of 'elif' here.

if and else in Python,if age is true grade is false,this program print is wrong

age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print "You can play this game."
else:
print "Sorry, you can't play the game."
if age is true and grade is false,this program prints wrong output.but if age is false, it prints correct output.
Why is it happening?
You are leaving open the possibility that age >= 8 but grade < 3 in which you have no control flow to handle. You can correct this succinctly with an and statement
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8 and grade >= 3:
print "You can play this game."
else:
print "Sorry, you can't play the game."
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print"You can play this game."
else:
print"Sorry , you can't play the game."
else:
print "Sorry , you can't play the game."
You have to include an 'else' condition in your nested if/else statement:
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print "You can play this game."
else:
print"Sorry, you can't play this game."
else:
print "Sorry, you can't play this game."

Categories