I wrote an if statement to check if the user input is “quit” and if so it will change the active variable/flag to False and this should exit my program.
All the other if statements work but when I enter “quit” it doesn’t exit the program. Instead, the if statement that I set to check for “quit” from the user input seems to be skipped and I keep getting an error:
age = int(age)
^^^^^^^^
ValueError: invalid literal for int() with base 10: 'quit'
My code is below:
prompt = "What is your age? "
prompt += "enter quit if you are finished "
active = True
while active:
age = input(prompt)
if age == 'quit':
active = False
age = int(age)
if age < 3:
print("Your tix is free")
elif age >= 3 and age <= 12:
print("Your tix costs $10")
else:
print("Your tix costs $15")
gru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You have to exit the loop with break
:
prompt = "What is your age? "
prompt += "enter quit if you are finished "
active = True
while active:
age = input(prompt)
if age == 'quit':
active = False
break
age = int(age)
if age < 3:
print("Your tix is free")
elif age >= 3 and age <= 12:
print("Your tix costs $10")
else:
print("Your tix costs $15")
To fix up your code, you don’t need active
:
prompt = "What is your age? "
prompt += "enter quit if you are finished "
while True:
age = input(prompt)
if age == 'quit':
break
age = int(age)
if age < 3:
print("Your tix is free")
elif age >= 3 and age <= 12:
print("Your tix costs $10")
else:
print("Your tix costs $15")