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 trial

Python

devin blair
devin blair
4,955 Points

Ticket program accepts negative values for requested tickets

I noticed when when requesting to buy a negative amount of tickets the total not only comes out to a negative value but it also adds to the tickets remaining var. so if ticket_remaining = 100 and you request -50 tickets, tickets_remaining will now be 150!. A classic IDOR if we actually accepted payment info, here was my fix for the bug.

TICKET_PRICE = 10
SERVICE_CHARGE = 2.25

tickets_remaining = 100  

def calcPrice(num_tickets):
    return (requested_tickets * TICKET_PRICE) + SERVICE_CHARGE 

while tickets_remaining >= 1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    name = input("what is your name? ")
    requested_tickets = input("how many tickets do you need? ")
    try:
        requested_tickets = int(requested_tickets)
        if requested_tickets > tickets_remaining or requested_tickets < 0:
          raise ValueError("invalid number or too many tickets requested, Please try again")
    except ValueError as err:
        print("{}.".format(err))
    else:
        amount_due = calcPrice(requested_tickets)
        print("Your total for {} tickets is ${} ".format(requested_tickets, amount_due))
        should_proceed = input("Confirm purchase? Y/N ")
        if should_proceed.lower() == "y":
            print("SOLD!")
            tickets_remaining -= requested_tickets
        else: 
            print("Thank you anyways, {}".format(name))
print("Sorry all of the tickets are sold out")

1 Answer

Hey devin blair! Thank you for sharing your debugging solution! It is always awesome to see how others solve various problems. Keep up the great work and happy coding, Pythonista!