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 trial

Python Python Basics All Together Now Gather Information

Jonathan Jimenez
seal-mask
.a{fill-rule:evenodd;}techdegree
Jonathan Jimenez
Python Development Techdegree Student 1,970 Points

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

boi
boi
14,241 Points

Apparently it's the same thing, doesn't matter.

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

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

I did the same, so when the user inputs anything other than an integer, like a char or float. It will throw a ValueError.