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

Why doesn't my service charge work?

<p>def calculate_price(number_of_tickets):
    return (number_of_tickets * TICKET_PRICE) + SERVICE_CHARGE

while tickets_remaining >= 1:
    print("There are {} tickets remaining.  ".format(tickets_remaining))
    name = input("What is your name?   ")
    num_tickets = input("How many tickets would you like, {}?  ".format(name))
    try:
        num_tickets = int(num_tickets)
        if num_tickets >= tickets_remaining:
            raise ValueError("There are only {} tickets remaining.".format(ticket_remaining))
    except ValueError as err:
        print("Oh no, we ran into an issue. Please try again.")
    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.lower() == "y":
            print("SOLD!")
            tickets_remaining -= num_tickets
        else:
            print("Thank you, {}".format(name))
print("Sorry the tickets are all sold out. :(")</p>
          ```

1 Answer

I'm assuming the paragraph tags are part of your post here and not your Python code. I'm also unsure of what you mean by the service charge not working. If you could provide clarification that would be great. I did find a few things to address however:

1) There are several variable declarations missing. For example:

tickets_remaining = 100
TICKET_PRICE = 10
SERVICE_CHARGE = 2

Did you post all of your code?

2) By including equal to here you will never be able to purchase all the tickets:

if num_tickets >= tickets_remaining:

To test try the total number of tickets as the first purchase. The condition should just be greater than.

3) There is a typo here:

raise ValueError("There are only {} tickets remaining.".format(ticket_remaining))

which results in:

NameError: name 'ticket_remaining' is not defined. 

The previously used variable is tickets_remaining with an s

Yes, I didn't post all my code. For me, if I input the number of tickets, it will just feedback the num_tickets * tickets_remaining, without the service charge. I feel a little bit strange, so I ask this question. Thanks for your help.