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

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

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

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:
> 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?

Returning a new value for my string variable

I am trying to update my string variable then return it to be printed.
race = 'human'
age = int(x)
def aging(age, maturity):
if x < 13:
maturity = 'Child'
elif x>13 and x<18:
maturity = 'Teenager'
elif x>18 and x<65:
maturity = 'Adult'
elif x>65 and x<99:
maturity = 'Senior'
else:
maturity = 'Dead'
aging(age, maturity)
x=int(input('Please enter age: '))
print ("Age is",age)
print ("You are a "+maturity)
The end product, however, is always
Please enter age: 9
Age is 9
You are a none
How can I get the maturity string to update?
Let's address your program as a whole -- the major issue I see is that your data is embedded in your code. Let's separate out the data to simplify the code:
MATURITIES = {
'a Child': (0, 12),
'a Teenager': (13, 17),
'an Adult': (18, 64),
'a Senior': (65, 99),
}
def aging(age):
for maturity, (lower, upper) in MATURITIES.items():
if lower <= age <= upper:
return maturity
return 'Dead'
age = int(input('Please enter age: '))
print("Your age is", age)
print("You are", aging(age))
You need to refactor your code - remove x, change where you define variables/take input, and return something (maturity):
def aging(age):
if age < 13:
maturity = 'Child'
elif age > 13 and age < 18:
maturity = 'Teenager'
elif age > 18 and age < 65:
maturity = 'Adult'
elif age > 65 and age < 99:
maturity = 'Senior'
else:
maturity = 'Dead'
return maturity
age = int(input('Please enter age: '))
maturity = aging(age)
print ("Age is" + age)
print("You are a " + maturity)
Output:
Please enter age: 9
Age is 9
You are a Child
Use return
def aging(age, maturity):
if x < 13:
maturity = 'Child'
elif x>13 and x<18:
maturity = 'Teenager'
elif x>18 and x<65:
maturity = 'Adult'
elif x>65 and x<99:
maturity = 'Senior'
else:
maturity = 'Dead'
return age, maturity
race = 'human'
age = int(input('Please enter age: '))
age, maturity = aging(age, maturity)
print ("Age is",age)
print ("You are a "+maturity)
I recommend your function to return maturity.
You can global the variable, but it is considered a bad practice.
I did a few corrections on your code, it should work fine like this:
def aging(age):
if age < 13:
maturity = 'Child'
elif age >= 13 and age <18:
maturity = 'Teenager'
elif age >= 18 and age <65:
maturity = 'Adult'
elif age >= 65 and age < 99:
maturity = 'Senior'
else:
maturity = 'Dead'
return maturity
age = int(input('Please enter age: '))
print ("Age is", age)
print ("You are a", aging(age))

Having user input take string and int? (Python)

prompt = "Enter your age for ticket price"
prompt += "\nEnter quit to exit: "
active = True
while active:
age = input(prompt)
age = int(age)
if age == 'quit':
active = False
elif age < 3:
print("Your ticket is $5")
elif age >= 3 and age < 12:
print("Your ticket is $10")
elif age >= 12:
print("Your ticket is $15")
This is some fairly simple code but I am having one issue. The problem is, for the code to run age has to be converted into an int. However, the program is also supposed to exit when you type in "quit". You can always have another prompt along the lines of "Would you like to add more people?". However, is there a way to make it run without having to prompt another question?
I would suggest getting rid of the active flag, and just breaking when "quit" is entered, like so, then you can safely convert to int, because the code will not reach that point if "quit" was entered:
while True:
age = input(prompt)
if age == "quit":
break
age = int(age)
if age < 3:
print("Your ticket is $5")
elif age < 12:
print("Your ticket is $10")
else:
print("Your ticket is $15")
Note that the age >= 3 and age >= 12 checks are unnecessary, because you have already guaranteed them with the earlier checks.
If you want to add another prompt, you can ask the first prompt before the loop and the other one at the end of it. And if you want to add the prices, you need a variable for it. If you dont want to prompt another question but want more user input, leave the prompt empty.
prompt = "Enter your age for ticket price"
prompt += "\nEnter 'quit' to exit: "
price = 0
user_input = input(prompt)
while True:
if user_input == 'quit':
break
age = int(user_input)
if age < 3:
price += 5
elif age < 12:
price += 10
else:
price += 15
print(f"Your ticket is ${price}")
user_input = input("You can add an age to add another ticket, or enter 'quit' to exit. ")

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