Python Python Basics All Together Now Cleaner Code Through Refactoring

walter fernandez
walter fernandez
4,991 Points

in 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.

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!
       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))
        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
           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! :-)

Steven Parker
Steven Parker
200,760 Points

Thanks, "mystery mod". :wink: And for those unfamiliar with Markdown, there's also a video on code formatting.

1 Answer

Steven Parker
Steven Parker
200,760 Points

You 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.