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

Darren Jones
Darren Jones
280 Points

Question 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
Steven Parker
217,557 Points

The 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.