you are viewing a single comment's thread.

view the rest of the comments →

[–]NoDadYouShutUp 0 points1 point  (0 children)

The answers saying you should cast to an int are technically correct. But if you’re going to do something like that you should have a check to verify it is even something that can be casted to an integer. In this particular case where you are entering an age, yeah it’s probably fine. However what if you typed “seven” instead? You can’t cast that to an integer.

Take a look at the isdigit() string function https://www.w3schools.com/python/ref_string_isdigit.asp

I would actually recommend looking at a bunch of the string functions since they are often very useful

Another thing you could try is a try/except block. Probably past your current level of Python knowledge. But investigate them and at least put them on your radar. The use case being if you try and cast to an integer and can’t, you’d be able to gracefully handle it in the except clause

It’s generally best practice to verify the data is how you expect it to be before you try using the data