Reputation: 1
I am working through a textbook assignment from Python Crash Course by Eric Matthes.
The code runs according to the instructions, but I wanted to fix three issues and do not know how. Issues are: (1) I do not know if I am using the flag correctly. (2) I used int() to manipulate the user input, so that I could compare the user value to integers. (3) If a user were to enter 'quit', the program would crash and show an error ( ValueError: invalid literal for int() with base 10: 'quit'). This is tied to my issue 2.
Thank you for your help!
David
-- Instructions: A movie theater charges different ticket prices depending on a person's age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is $10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket.
prompt = "\nI will price your ticket. What is your age?"
active = True #Per Matthes, using the flag Active, a program "should run while
#the flag is set to True and stop running when any of several events sets the
#value of the flag to False."
while active:
message = int(input (prompt))
if message < int(3):
print("Your ticket is free!")
elif int(3) <= message <= int(12):
print("Your ticket is $10!")
elif message > int(12):
print("Your ticket is $15!")
else:
active = False
break #Per Matthes, the break statement will force the program "to exit
#a while loop immediately without running any remaining code in the
#loop."
Upvotes: 0
Views: 357
Reputation: 1
I'm working through the same textbook as well and found a solution using just the information provided within the section. I broke up that int()
function and then utilized 'continue' and separated out the other check for age to a separate if/else block:
prompt = "\nPlease tell us your age before purchasing a ticket:"
prompt += "\n(Please type 'quit' when finished.) "
active = True
while active:
age = input(prompt)
if age == 'quit':
print("Enjoy your movie!")
active = False
continue
else:
age = int(age)
if age < 3:
print("Your ticket is free!")
elif age <= 12:
print("Your ticket is $10.")
else:
print("Your ticket is $15.")
Upvotes: 0
Reputation: 1
I'm following the same book, albeit the 2nd Edition. My solution was the following, I avoided Exception Handlers, and other stuff and kept it simple:
Basically, you have a branch of:
if age == 'quit': active = False
, where age is 'quit'.
Then another branch comprised of an else block where age is NOT 'quit'.
prompt = ("\nPlease, enter your age: \n")
prompt += ("\nFeel free to exit the program by typing
'quit': ")
active = True
while active:
age = input(prompt) #Or age = int(input(prompt))
if age == 'quit':
active = False
else:
age = int(age)
if age < 3:
print("\nYour ticket is free!")
elif age < 12:
print("\nYour ticket cost is $10.")
elif age > 12:
print("\nYour ticket cost is $15.")
Upvotes: 0
Reputation: 3631
So, the comments say as much, but here's the full explanation:
The user is asked to enter an age. No matter what age you enter, it met by one of the three conditions, so no age entered will trigger the 'else' condition.
The only way to trigger the else is to enter a non-integer such as a word or letter, but the line message = int(input (prompt))
tries to convert the input to an integer, so entering anything else will throw an exception.
You can solve this with an exception handler, or by implementing a specific integer that executes the quit response, such as zero.
Here's an example with an exception handler:
prompt = "\nI will price your ticket. What is your age?"
active = True
#Per Matthes, using the flag Active, a program "should run while
#the flag is set to True and stop running when any of several events sets the
#value of the flag to False."
while active:
message = input (prompt)
try:
message = int(message)
if message < int(3):
print("Your ticket is free!")
elif int(3) <= message <= int(12):
print("Your ticket is $10!")
elif message > int(12):
print("Your ticket is $15!")
except:
active = False
print("\nGoodbye!")
Here's one where entering zero will trigger the quit:
prompt = "\nI will price your ticket. What is your age?"
active = True
#Per Matthes, using the flag Active, a program "should run while
#the flag is set to True and stop running when any of several events sets the
#value of the flag to False."
while active:
message = int(input (prompt))
if int(1) <= message < int(3):
print("Your ticket is free!")
elif int(3) <= message <= int(12):
print("Your ticket is $10!")
elif message > int(12):
print("Your ticket is $15!")
else:
active = False
print("\nGoodbye!")
Upvotes: 1
Reputation: 23
You've could tried using Exception Handling.
try:
message = int(input (prompt))
except:
print("You've entered an invalid input")
break
if this is you're problem this would solve it.
Upvotes: 0