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 Gather Information

Weird Math display

So my code on total = number_tickets * TICKET_PRICE while displaying in my print statement instead of giving out say 30 for 3 tickets, it outputs 333

I'm not sure what's wrong?


tickets_remaining = 100

Output how many tickets are remaining using the tickets_remaining variable

print("There are {} left to purchase!".format(tickets_remaining))

Gather the users name and assign it to a new variable

usersname = input("please enter your name here: ")

prompt the user by name and ask how many tickets they would like

number_tickets = input("Hello {} how many tickets are you interested in today? ".format(usersname))

calculate the price (number of tickets by price) and assign to a variable

total = number_tickets * TICKET_PRICE print("Ok {}, {} tickets at 10 bucks a ticket will run you {}".format(usersname, number_tickets, total), "still interested?")

output the price to the screen

2 Answers

Steven Parker
Steven Parker
210,425 Points

The result of an "input" is a string, so that's what is in "number_tickets". And when you "multiply" a string by a number, you get that string repeated as many times as the number.

To get the expected value, convert the string to a number with the "int()" function.

Steven Parker
Steven Parker
210,425 Points

It's the input value that needs to be converted into a number before multiplying:

    total = int(number_tickets) * TICKET_PRICE

And for future postings, use Markdown formatting to preserve code appearance, or share the entire workspace by making a snapshot and posting the link to it.

so I want to write it was total = int(number_tickets * TICKET_PRICE)

I thought I tried that and got the same results. I'll try again tonight when I get off of work

Derek McLane
Derek McLane
11,185 Points

I had the same issue. It was writing the amount of tickets as a string and multiplying it by the ticket price, so it just wrote the answer out a bunch of times.

What I did was write it as

tickets_wanted = int(input("Hello {}, how many tickets would you like? ".format(name))

cost = int(tickets_wanted * ticket_price)