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

Service charge not defined

Hey guys here below is my code exactly like it was in the video but is giving me SERVICE_CHARGE not defined. Could anybody please tell me where the mistake is?

SERVICE_CHARGE = 2 TICKET_PRICE = 10

tickets_remaining = 100

def calculate_price(number_of_tickets): return (number_of_tickets * TICKET_PRICE) + SERIVICE_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(tickets_remaining)) except ValueError as err: print("Oh no,we ran into an issue.{}. Please try again".format(err)) else:
amount_due = calculate_price(num_tickets) print("the total due is ${}".format(amount_due)) should_proceed = input("would you like to prceed {} (y/n)".format(name)) 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!!! :(")

6 Answers

  • I did not work because you use ''' instead of ```, but anyways, I found few issues with your code and I fixed them, look over the code and compare it to yours
SERVICE_CHARGE = 2
TICKET_PRICE = 10

tickets_remaining = 100

def calculate_price(number_of_tickets):
    return (number_of_tickets * TICKET_PRICE) + SERVICE_CHARGE

# tickets_remaining > 0 would be better option than tickets_remaining >= 1
while tickets_remaining > 0:
    print("There are {} tickets remaining.".format(tickets_remaining))
    name = input("what is your name? ")
    # Making sure that the number we get back is an integer
    num_tickets = int(input("How many tickets would you like {}? ".format(name)))
    try:
        if num_tickets > tickets_remaining:
            raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
        print("Oh no,we ran into an issue.{}. Please try again".format(err))
    else:
        amount_due = calculate_price(num_tickets)
        print("the total due is ${}".format(amount_due))
        should_proceed = input("would you like to prceed {} (y/n)".format(name))
        if should_proceed.lower() == "y":
            print("SOLD!")
            tickets_remaining -= num_tickets
        else:
            print("thank you {}!".format(name))

# Print this only when the while loop fails/
print("Sorry the tickets are all sold out!!! :(")

Geez so sorry about that and thank you so much,Stivan, for dealing with me. Thank you again!

# These should be in their own line
SERVICE_CHARGE = 2
TICKET_PRICE = 10

So sorry Stivan but i wasn't able to share the code properly... in my workspace the code is actually like you wrote it, but when I posted the question the formatting changed. Could you please tell me how to share the code in the workspace so maybe you'll see other possible mistakes? Thank you so much and sorry again.

# Paste your code inside these tags
# ```python

# ```

# Ignore the #, they are just for comments

Stivan - thanks so much for your response. Here's the code again, and I've tried to do what you recommended, but it is still not displaying properly. I'm not sure what I'm doing wrong.

' ' ' python SERVICE_CHARGE = 2 TICKET_PRICE = 10

tickets_remaining = 100

def calculate_price(number_of_tickets): return (number_of_tickets * TICKET_PRICE) + SERIVICE_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(tickets_remaining)) except ValueError as err: print("Oh no,we ran into an issue.{}. Please try again".format(err)) else:
amount_due = calculate_price(num_tickets) print("the total due is ${}".format(amount_due)) should_proceed = input("would you like to prceed {} (y/n)".format(name)) 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!!! :(")'' ' ' '