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 trialJonathan Jimenez
Python Development Techdegree Student 1,970 Pointsdifferent approach to asking number of tickets, instead num_tickets = int(input("{}, how many tickets do you want to buy
so prompting num_tickets = int(input("{}, how many tickets do you want to buy? ".format(name)))
Code line still works as it should, but i am wondering if the way i wrote it not recommended. Such as having input inside the int() method.
2 Answers
Josh Keenan
20,315 PointsThese are similar approaches but yours is the better approach. Instead of taking input, and converting it to an integer, you are enforcing that the input must be an integer. In the original you can input a string, and then try to convert it to an integer, whereas in your method you are ensuring the user is inputting an integer.
Personally I will always cast the input I get into a type straight away, to ensure there is the right user input and not something that's gonna break my code down the line.
Sumiya Malik
3,976 PointsI did the same, so when the user inputs anything other than an integer, like a char or float. It will throw a ValueError.
boi
14,242 Pointsboi
14,242 PointsApparently it's the same thing, doesn't matter.