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 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?

TICKET_PRICE = 10

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
231,275 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
231,275 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)