r/PythonLearning • • 17d ago

Help Request Need help understanding how != works

Hello, I'm working on learning the basics of try/except blocks. To do so, I'm intentionally raising errors. The question is: why is my TypeError occurring when I enter an integer? I checked the input type, and it shows as an integer, but the error is still raised. What am I missing here?

31 Upvotes

17 comments sorted by

View all comments

24

u/Ok-Promise-8118 17d ago

You are comparing the variable to the type int. You want to compare the type of the variable to the type int. You already have the concept in your print statement.

10

u/The-Turnt-Tiger 17d ago
if type(carsAvaliable) != int:

This has been driving me nuts. Thank you so much for the explaination! 

8

u/fstorino 17d ago

So either:

    if isinstance(carsAvaliable, int):

Or:

    if type(carsAvaliable) is int: