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 trialDarren Jones
280 PointsQuestion regarding changing string to integer. Different method, same result?
When I done the task myself, I used the following line of code:
# Prompt the user by name and ask how many tickers they would like
number_of_tickets = int(input("Hello {}, how many tickets would you like to buy today? ".format(name)))
But I noticed the teacher chose to do it a slightly different way where he converted the string to an integer on a separate line such as:
# Prompt the user by name and ask how many tickers they would like
number_of_tickets = input("Hello {}, how many tickets would you like to buy today? ".format(name))
number_of_tickets = int(number_of_tickets)
I'm just wondering if there is any difference between the two methods and was I 'wrong' for doing it my way, as it seemed to work ok?
Thank you
TICKET_PRICE = 10
tickets_remaining = 100
#Output how many tickets are remaining
print("There are {} tickets remaining".format(tickets_remaining))
# Gather the users name and assign it to a new variable
name = input("What is your name? ")
# Prompt the user by name and ask how many tickers they would like
number_of_tickets = int(input("Hello {}, how many tickets would you like to buy today? ".format(name)))
# Calculate the price (number of tickets multiplied by the price) and assing it to a variable
cost_of_tickets = number_of_tickets * TICKET_PRICE
# Output the price to the screen
print("{} tickets will cost a total of ${}".format(number_of_tickets, cost_of_tickets))
2 Answers
Steven Parker
231,261 PointsThe end result is exactly the same, the teacher most likely broke it down into individual steps for extra clarity. You'll find that that lesson examples can often be made more compact and efficient when the focus is entirely on functionality and illustrating the concepts is not a concern.
Darren Jones
280 PointsThanks Steven