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 trialFaraz Jahangiri
1,028 PointsWhy is there a type error for the ">" sign?
TICKET_PRICE = 23.99
tickets_remaining = 100
while tickets_remaining >= 1: user_name = input("Please enter your name: ") print("There are {} tickets remaining.".format(tickets_remaining))
amount_purchased = input("How many tickets would you like {}? ".format(user_name))
try:
if amount_purchased > tickets_remaining:
raise ValueError("That is not possible, sorry")
amount_purchased = int(amount_purchased)
except ValueError:
print("That is not a valid value, please enter a number")
else:
total_cost = TICKET_PRICE * amount_purchased
print("your total for this is £{}".format(total_cost))
proceed = input("Do you want to proceed with this sale, PLease enter yes or no: ")
if proceed == "yes":
print("SOLD!!")
tickets_remaining -= amount_purchased
elif proceed == "no":
print("Ok, Thank you anyway!")
else:
print("That is not a valid answer")
print("We are so sorry but we are all out of tickets")
The problem is, I am trying to raise an exception Error but the code stops running after the if statement which says, "amount_purchased > tickets_remaining?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Faraz Jahangiri ! The problem here is that you cannot compare a string to an integer in this way. The input that the user gives will be of type string. And tickets_remaining
is an integer. To compare them, you will first need to convert the user's input to an int
.
On line 9 try this instead:
# convert the user's input to an integer for comparison
amount_purchased = int(input("How many tickets would you like {}? ".format(user_name)))
Hope this helps!
nakalkucing
12,964 Pointsnakalkucing
12,964 PointsHi Faraz Jahangiri! I'd like to help, but your code isn't in proper syntax. While I can correct the syntax in my workspace, I can't be sure that it's set up the same way as yours.
Hope this helps! :)