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 trialTsering Choedhen
1,051 PointsDo we always have to convert the input value to integer (int)?
The following were my answer (where in I have not converted the input value to integer and that's why i was getting wrong amount). My question is do we have to convert the input value to int ALWAYS?
print ("There are {} tickets remaining.".format(tickets_remaining))
name = input("What is your name? ")
number_of_tickets = input("Hey {}, how many tickets you want today? ".format(name))
Total_Amount = (TICKET_PRICE)*(number_of_tickets)
print ("Your total purchase amount is ${}".format(Total_Amount))
1 Answer
Steven Parker
231,275 PointsAs Erlend observed, converting the input is necessary if it contains a number and you want to do math or comparisons on it. But the conversion might not always be to "int", for example the input might be money and you would convert it to "float" instead.
Conversion is not needed if the input is not a number, for examples something like a person's name or a color.
name = input("What is your name? ") # no need to convert here
age = int(input("What is your age? ")) # convert input to a number
if (age >= 21):
print(f"Great, {name}, let's go get a beer!")
else:
print(f"Great, {name}, let's go get some ice cream!")
Tsering Choedhen
1,051 PointsThank you Steven Parker, for the clarification and also additional informations. And also thank you Erlend Dybvik Indrelid for your explanation and support.
Benyamin Kohanchi
Courses Plus Student 2,342 PointsVery helpful, as was Erlend Dybvik Indrelid. Thanks!
Erlend Dybvik Indrelid
5,843 PointsErlend Dybvik Indrelid
5,843 PointsHi! This depends on what you are supposed to do with the input value. In this case you need to convert the number_of_tickets to an integer value, because you are using the value later to compute how much the total cost will be. Remember: an integer is a whole number, however a string does not need to be a number, but is just a piece of character(s). If you would have not converted the input value to an integer, you would have multiplied the string value of the number_of_tickets with the ticket_price. This would not be what you want the program to do. Here is an example:
I hope this answered your question! Be sure to let me know if there is something you don't understand.
-Erlend