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 trialwalter fernandez
4,992 Pointsin my last if statement. It does not matter if I input yes or not. my code always output "SOLD OUT". I don't know why.
SERVICE_CHARGE = 2
TICKET_PRICE = 10
tickets_remaining = 100
#create the calculate function, it take the number tickets and return
# return TICKET_PRICE * num_tickets
def calculate_price(number_of_tickets):
return TICKET_PRICE * number_of_tickets + SERVICE_CHARGE
while tickets_remaining >= 1 :
print("there are {} tickets".format(tickets_remaining))
name = input("what is you name? ")
num_tickets = input ("how many tickets would you like {}? ".format(name))
#Expect a ValueError to happen and handle it appropiatly...remember to test it out!
try:
num_tickets = int(num_tickets)
#Raise a ValueError if the request is for more tickets than there are available.
if num_tickets > tickets_remaining :
raise ValueError("Sorry, There are only {} tickets remaining".format(tickets_remaining))
except ValueError as err:
#Include the error text in the output.
print("Oh no!, you don't know what an integer is. {} please try again".format(err))
else:
amount_due = calculate_price(num_tickets)
print("the total due is {}".format(amount_due))
should_proceed = input("do you want to proceed? Y/N ")
if should_proceed == "yes" or "Yes" or "YES":
print("SOLD OUT")
tickets_remaining -= num_tickets
else:
print("thanks, {}, have a nice day".format(name))
print("the tickets are SOLD OUT")
MOD: I just updated your question to put your code in Markdown format so it's much easier for everyone to read, and to help you out! See the Markdown Cheatsheet that is linked just below the textbox when you're typing a question, to format code. Don't worry, it's easy! :-)
1 Answer
Steven Parker
231,236 PointsYou need to use complete expressions on both sides of any logic operation (like "or"). A non-empty string by itself will be evaluated as "truthy".
But a more compact test for multiple versions of the same word can be done using case conversion:
if should_proceed.lower() == "yes": # will also match "Yes", "YES", etc.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThanks, "mystery mod". And for those unfamiliar with Markdown, there's also a video on code formatting.