Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Branch and Loop

Anthony Costanza
Anthony Costanza
2,123 Points

What if there are 50 tickets left and someone puts in an order to purchase 51 - this code will say it is sold out when

What if there are 50 tickets left and someone puts in an order to purchase 51 - this code will say it is sold out when that's not really true - how do you code it so it says your order exceeds the 50 tickets remaining?

3 Answers

Hi Anthony

Here is a version of my solution (which goes beyond the challenge) with error handling:

TICKET_PRICE = 10

tickets_remaining = 100  

while tickets_remaining >= 1:
    if tickets_remaining > 1:
        verb = "are"
        remaining_s = "s"
    else:
        verb = "is"
        remaining_s = ""
    print("There {} {} ticket{} remaning (at $10.00 per ticket).".format(verb, tickets_remaining, remaining_s))
    valid = False
    while valid != True:
        client_name = input("What is your name? ")
        if (len(client_name) == 0):
            print("*** INVALID SELECTION ***")
        else:
            try:
                val = int(client_name)
            except ValueError:
                valid = True
            else:
                print("*** INVALID SELECTION ***")
    valid = False
    while valid != True:
        num_ticket = input("{}, how many tickets do you want to purchase? ".format(client_name))
        try:
            val = int(num_ticket)
        except ValueError:
            print("*** INVALID SELECTION ***")
            continue
        if val <= tickets_remaining:
            valid = True
        else:
            print("*** ERROR ***: There {} only {} ticket{} remaining. Try again.".format(verb, tickets_remaining, remaining_s))
    num_ticket = int(num_ticket)
    if num_ticket > 1:
        num_s = "s"
    else:
        num_s = ""
    amount_due = num_ticket * TICKET_PRICE
    print("The total amout due is ${}.00 for {} ticket{}.".format(amount_due, num_ticket, num_s))
    user_info = input("Do you want to procced Y/N? ").lower()
    if user_info == "y":
        print("SOLD!!!")
        tickets_remaining -= num_ticket
    else :
        print("Thanks anyways, {}".format(client_name))
print("Tickets soldout! :( ")

From this thread:

https://teamtreehouse.com/community/can-someone-help-me-with-this-python-script

You can test it here:

https://www.katacoda.com/courses/python/playground

II hope that helps.

Stay safe and happy coding!

I am a new python student. I understand most of your code. I have a problem with a part of your code where is : Valid = False . Why are you using always valid = false? Can you shortly explain it or send me a link with some info ? Thank You!

Your question is good/sound/understandable.

I set value=false before I prompt the user for input. It stays false until the user provides a valid response (and otherwise gives an error message). And it keeps looping asking for a valid response indefinitely until a valid one is entered. Does that make sense?

I hope that helps.

Stay safe and happy coding!