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 trialAsh Skelton
3,745 PointsWhy reassign variable rather than combine?
Why would you reassign the variable to make it an int?
num_tickets = input("How many tickets would you like, {} ".format(name))
num_tickets = int(num_tickets)
Would this not be cleaner?
num_tickets = int(input("How many tickets would you like, {} ".format(name)))
Is there a reason to choose one method over the other in this scenario?
1 Answer
Steven Parker
231,271 PointsBoth versions are equivalent as long as no errors occur. Doing the steps separately might be useful for debugging.
For example, if the conversion should fail (as if the word "blue" was given for input), then in the first example "num_tickets" would sill have the input that caused the problem. But in the second example the input would not be saved anywhere.
Also, for the purposes of the videos, steps are often performed separately for clarity where in practice they can be consolidated to make the code more compact and efficient.
Ash Skelton
3,745 PointsAsh Skelton
3,745 PointsGot it, thanks Steven!
In terms of your comment around steps being performed separately for clarity - this makes sense and is definitely useful to see how the flow should work, however, what may be just as useful is if the Treehouse teachers did a "clean up" session for the code maybe after the quizzes/challenges. That way, the student learns what exactly the code is doing in an easy to read flow, but also teaches you where you can consolidate some of the code and create efficiencies which would be beneficial to know in "real world" scenarios!
Thanks again
Steven Parker
231,271 PointsSteven Parker
231,271 PointsPerhaps there's an opportunity for a "code optimization" course or workshop. You might send in a suggestion!