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

Jonathan Jimenez
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.

8,669 Points

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

2 Answers

Josh Keenan
Josh Keenan
19,426 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.

Sumiya Malik
Sumiya Malik
3,964 Points

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