Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Cleaner Code Through Refactoring

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
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:

TICKET_PRICE = 10
SERVICE_CHARGE = 2

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))
    try:
        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))
    else:
        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.
            print("SOLD!")
            tickets_remaining -= ticket_amount
        else:
            print("Your order has been cancled. Thank you, {}!".format(name))
print("We just sold out!!")

1 Answer

Jennifer Nordell
STAFF
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: