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 Python Basics All Together Now Cleaner Code Through Refactoring

I don't know where I've made the mistake but my code keeps resulting in the total being incorrect

I think I have basically the same code as Craig does in the vid but my price calculator never produces the right sum for the number of tickets. Does anybody know how to fix it?

Justin Horner
Justin Horner
Treehouse Guest Teacher

Hello, Agbo

We need to see your code in order to help debug the issue. Please provide your code directly in this post or link to your Workspace.

Thank you!

Hi Justin, here is my code. I think the calculation is multiplying by the tickets remaining instead of ticket price but I'm not sure how to fix it.

SERVICE_CHARGE = 2
TICKET_PRICE = 10    

tickets_remaining = 100  

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?  ")
    tickets_required = input("How many tickets would you like, {}?:  ".format(name))
    try:
        tickets_required = int(tickets_required)
        if tickets_required > 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(tickets_remaining)
        print("The total due is £{}".format(amount_due))
        confirmation = input("Would you like to proceed with your purchase? Y/N  ")
        if confirmation.lower() == "y":
            print("SOLD!")
            tickets_remaining -= tickets_required
        else:
            print("Thank you for visiting our website {}!".format(name))
print("Sorry we are all sold out! :(")
Patricia Hector
Patricia Hector
42,902 Points

There is a tiny error in the first line of the else block, you used tickets_remaining instead of tickets_required to calculate amount_due(..)

1 Answer

Patricia Hector
Patricia Hector
42,902 Points
SERVICE_CHARGE = 2
TICKET_PRICE = 10    

tickets_remaining = 100  

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?  ")
    tickets_required = input("How many tickets would you like, {}?:  ".format(name))
    try:
        tickets_required = int(tickets_required)
        if tickets_required > 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(tickets_required) # ======>  CORRECT VARIABLE: tickets_required
        print("The total due is £{}".format(amount_due))
        confirmation = input("Would you like to proceed with your purchase? Y/N  ")
        if confirmation.lower() == "y":
            print("SOLD!")
            tickets_remaining -= tickets_required
        else:
            print("Thank you for visiting our website {}!".format(name))
print("Sorry we are all sold out! :(")

Thank you Patricia! I realised where I went wrong as I was writing my question 🤦🏾‍♂️