Python Python Basics All Together Now Cleaner Code Through Refactoring

Agbo Lamina
Agbo Lamina
1,194 Points

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!

Agbo Lamina
Agbo Lamina
1,194 Points

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,899 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,899 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! :(")
Agbo Lamina
Agbo Lamina
1,194 Points

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