Python Python Basics All Together Now Cleaner Code Through Refactoring

keith frazier
keith frazier
Python Development Techdegree Student 5,771 Points

Calculate_price is only adding strings and returning the wrong value.

When I run my code I get no errors, but the value is always incorrect. It is adding string types in the function but I can't find where the int is converted to a string. If I ask for 100 tickets, I'm told the amount due is 1002. Here is my code:


tickets_remaining = 100

#Creat the calculate price function. It takes ticket amount and returns
#   ticket amount * ticket price
def calculate_price(qty_of_tickets):
    return qty_of_tickets * TICKET_PRICE + SERVICE_CHARGE

while tickets_remaining >= 1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    name = input("What is your name?   ")
    ticket_amount = input("Hello {}, how many tickets would you like?   ".format(name))
        ticket_amount = int(ticket_amount)
        if ticket_amount > tickets_remaining: 
            raise ValueError("We only have {} tickets available.".format(tickets_remaining))
    except ValueError as err:
        print("Uh-Oh, something went wrong. {} Please try again.".format(err))
        total_cost = calculate_price(ticket_amount)
        print("The total cost due is {}".format(total_cost))
        buy_or_not = input("Would you like to proceed?  Y/N  ") 
        if buy_or_not.lower() == "y":
            #TODO: Gather credit card information and process it.
            tickets_remaining -= ticket_amount
            print("Your order has been cancled. Thank you, {}!".format(name))
print("We just sold out!!")

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi, keith frazier ! No, it isn't adding string types :smiley: The formula is the number of tickets (100) times the ticket price (10) plus the service charge(2). 100 x 10 = 1000. Add 2. The answer is 1002!

Your code is fine!

Hope this helps! :sparkles: