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.

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

2 Answers

Josh Keenan
Josh Keenan
19,377 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
1,405 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.