Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlok Yadav
1,023 PointsCan someone point out my error in this code?
This is the part in the video where he asks to make an exception for the ValueError. I kind of Frankensteined this but it works well until the end, where it automatically prints "We're sorry! All the tickets have sold out!" even when that's not true.
Thanks in advance.
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1:
print("There are {} tickets remaining for sale.".format(tickets_remaining))
user_name = input("\nHello! What is your name? ")
while True:
ticket_number = input("Hello {}! How many tickets would you like to purchase? ".format(user_name))
# Expect a ValueError to happen and handle it appropriately.
try:
total_price = int(ticket_number) * TICKET_PRICE
print("\nYour total will be ${}.".format(total_price))
break
except ValueError:
print('\nOops! Please enter your response in numerals.\n')
continue
proceed = input("Would you like to confirm your sale of {} tickets for ${}? Y/N ".format(ticket_number, total_price))
if proceed.lower() == "y":
print("\nAwesome! Your tickets have been sold!\n")
tickets_remaining -= int(ticket_number)
break
#TODO: Gather and process CC information.
else:
print("\nNo problem! Let's try again.\n")
continue
print("We're sorry! All the tickets have sold out.")
2 Answers
jb30
44,807 Points proceed = input("Would you like to confirm your sale of {} tickets for ${}? Y/N ".format(ticket_number, total_price))
if proceed.lower() == "y":
print("\nAwesome! Your tickets have been sold!\n")
tickets_remaining -= int(ticket_number)
break
When you use break
here, you are breaking out of the while loop formed at while tickets_remaining >= 1:
, so your program does not recheck the condition before continuing below the while loop. If you comment out or remove the break
from this section, the while loop will continue to run and will check if tickets_remaining >= 1
.
Tanzila Rafeeq
2,936 Pointsyou need to put away break, and you don't need continue
Alok Yadav
1,023 PointsThanks for responding, I appreciate it! Follow up question: I enter continue there because I want it to loop back (is that the right term?) to the point where they enter the number of tickets. Don't I need to put a continue for that to happen?
Alok Yadav
1,023 PointsAlok Yadav
1,023 PointsThanks so much for answering this!!