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 trialDaron Anderson
2,567 PointsCan I receive help with errors?
import sys
Constant/ Ticket Price is 19 >> int
TICKET_PRICE = 10
declare & assign Ticket counter to int
tickets_remaining = 100
Run this code until there is no tickets remaining
while tickets_remaining >= 1: your_name = input("Hello, Welcome to MasterTicket!!, Your name is? ")
# Output how many tickets are remaining using the ticket counter var
print ("There are currently {} tickets left.".format(tickets_remaining))
# Gather users name and assign it to a new variable
# Error Handling Block VVVVVV
# Expect a ValueError to occur , handle it appropriateky....test it
try:
buy_ticket = int(input("OK {}, How many tickets to buy? ".format(your_name)))
#Raise Value Error for exceeded limit ticket purchase
if buy_ticket > tickets_remaining:
raise ValueError("There are only {} tickets available.. Sowwy.".format(tickets_remaining))
except ValueError as err:
# Include err text int the output
print ("Some thing is wrong {}".format(err))
# End Error Handling ^^^^^
# You want this code to run if you know how many tickets there are
else:
Result = buy_ticket * TICKET_PRICE
print ("Your total will be ${}".format(Result))
print ("Tell your friends before we run out!!")
Cont = input("Would you like to continue {} ? Y/n ".format(your_name))
if Cont.lower() == "y":
#TODO: Gather CreditCcard Info and process it
print ("Sold!!")
# Reduce amount of tickets remaining by the amount sold
tickets_remaining -= buy_ticket
elif Cont.lower() == "n":
print ("Too bad? Maybe Later? Thanks anyways!!")
sys.exit(0)
# End greeting by thanking by name
print ("Thank you for shopping with us!")
Notify the user no more tickets are available
print ("Sorry the tickets are sold out.. :(")
### I get the errors printed twice any help???
ERRORS: OK Daron, How many tickets to buy? blue
OUTPUT:Some thing is wrong invalid literal for int() with base 10: 'blue'
I guess this ones kind of no ERROR: OK daron, How many tickets to buy? 24354
OUTPUT:Some thing is wrong There are only 100 tickets available.. Sowwy.
Kayc M
12,593 PointsHey Daron, I tried to fix your code without changing it too much so you can see what you might have missed, hope it helps you out.
# Output how many tickets are remaining using the ticket counter var
import sys
TICKET_PRICE = 10
tickets_remaining = 100
#You forgot to write your code inside the while function
while tickets_remaining >=1:
print("There are currently {} tickets left.".format(tickets_remaining))
your_name = input("Hey there welcome, what's your name? ")
# You forgot to input user's name ^^^^
#You could've blocked the error message only using Try and Except
try:
buy_ticket = int(
input("OK {}, How many tickets to buy? ".format(your_name)))
if buy_ticket >= tickets_remaining:
print("Oh no, we're out of stock ")
sys.exit()
#In case the user tries to buy more than our stock ^^
elif buy_ticket <= 0:
print("You need to insert more than 0 ")
sys.exit()
#In case the user tries to insert a negative number ^^
except ValueError as err:
err = print("Something is wrong")
sys.exit()
#In case the user tries to type something else
result = buy_ticket * TICKET_PRICE
# You could've also printed the results without using try or else
print("Your total will be ${}".format(result))
print("Tell your friends before we run out!!")
Cont = input("Would you like to continue {} ? Y/n ".format(your_name))
if Cont.lower() == "y":
# TODO: Gather CreditCcard Info and process it
print("Sold!!")
# Reduce amount of tickets remaining by the amount sold
tickets_remaining -= buy_ticket
elif Cont.lower() == "n":
print("Too bad? Maybe Later? Thanks anyways!!")
sys.exit()
# End greeting by thanking by name
print("Thank you for shopping with us!")
#Last line out of the while function so it prints it once the code is over
2 Answers
Daron Anderson
2,567 Points@Kayc Mendez When you ran it did it loop through when thrown errors?
Kayc M
12,593 PointsThis fixed one? no
Daron Anderson
2,567 Points@kayc Mendez ok no prob I was writing it my own way when he was asking to pause the video, It was working and then I lost my mind somehow and yea.. Thanks!
Daron Anderson
2,567 PointsDaron Anderson
2,567 PointsI kind of wrote it different because of the pasusing. I didnt break anything did I?