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

I don't know why the multiplication doesn't work on this....whats wrong here?

here is the code

TICKET_PRICE = 10

tickets_remaining = 100

output how many tickets left

print("There are {} tickets remaining".format(tickets_remaining))

gather user name and assign it to a new variable

name = input("What is your name? ") user_name = name

prompt the user by name and ask how many tickets they want

tickets = input("Hi {} how many tickets do you want? ".format(user_name))

calculate price (number of tickets * price) assign to a variable

price = int(tickets * TICKET_PRICE)

Output the price to the screen

print("The price will be {}".format(price))

it keeps repeating. If I say I want 10 tickets it comes out as 10101010101010101010 instead of the price

2 Answers

Here tickets is a string:

tickets = input("Hi {} how many tickets do you want? ".format(user_name))

The result of multiplying a string by (in this case) 10 will be to repeat the string 10 times.

You should convert the input to an integer to get the correct value

tickets = int(input("Hi {} how many tickets do you want? ".format(user_name)))

so giving a integer to a string causes it to multiply the string by the integer...I did not know that. Thanks!

You are converting the string * TICKET_PRICE so the result is the same. If you did this however:

price = int(tickets) * TICKET_PRICE

you would be multiplying two numbers

so doing it in that format does not work because TICKET_PRICE = 10 which is the same as "10" so it is a string and does that repeating thing again.... price = int(tickets) * int(TICKET_PRICE) works but whats different about that vs price = int(tickets * TICKET_PRICE) isn't that essentially the same?