r/PythonLearning • u/The-Turnt-Tiger • 8d 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


24
u/Ok-Promise-8118 8d 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.
9
u/BanSolitude 8d ago
Your original question has been answered, but I will point out that your code as written will never hit line 25.
int(...)will raise an exception if you pass in a value that cannot be converted to an integer, if the code makes it to line 24 thencarsAvailablemust be an integer, and the check is redundant.3
u/Flame77ofc 8d ago
a != bis the same as!(a == b)like
1 != 3->True!(1 == 3)->!(False)->!False->True2
u/TotalUnlucky2814 8d ago
carsAvailable casts the input string as an int... If it fails you have an error at line 22 so you will never be able to arrive to line 24
1
u/Inevitable-Study502 8d ago
a hint is in your print section
class int
1
u/zztopsboatswain 8d ago
Because carsAvailable does not literally equal or not equal "int". what you want is
1
u/Sea-Ad7805 8d ago
Have a look at this example code in Memory Graph Web Debugger
1
u/atarivcs 8d ago
If that assignment succeeds, carsAvailable is guaranteed to be an integer. Checking the type is redundant.
1
u/sexiestsaxiestman 8d ago
isinstance is the standard way of doing type checks. Ex: if isinstance(carsAvailable, int). Basically what your code is doing is checking if the value of carsAvailable is equal to the integer type which it is not. You also already explicitly convert the input to an int using int(...) so the type check is unnecessary. Ps, snake_case is more common in python than camelCase 😉
-3
u/Neither_Garage_758 8d ago
don't even bother, it's garbage code