When you try to do something automatic in Python, you get the TypeError: ‘<‘ Not Supported Between Instances Of ‘str’ And ‘int’ error message. Don’t be too frightened. This tutorial would take the approach to fix these problems in this famous software.
When Will The “TypeError: ‘<‘ Not Supported Between Instances Of ‘str’ And ‘int’” Error Happen?
Take a look at an example code from a programmer below.
usr_age = input("What is your Age? ")
if numerical_grade < 20:
msg = "You are not allowed!!"
else:
msg = "Go Ahead"
print(msg)
Of course, the above error message will appear. The reason is that you’ll get a string as input, and you’ll try to compare it to an int. So we will have two simple ways to solve this problem.
How to fix “TypeError: ‘<‘ Not Supported Between Instances Of ‘str’ And ‘int’” Error?
Two Effective Techniques.
To correct this fundamental error, use the syntax shown below.
Solution 1
You need to convert the input “string” to one int object. Let’s take a look at the following example.
Initially, we have a line like this:
usr_age = input("What is your Age? ")
You will fix it like this, very simple right!
usr_age = int(input("What is your Age? "))
Solution 2
If you try to convert your string to integer, you might use the syntax try catch. The proper way should be:
try:
input_var = int(user_input)
except ValueError as err:
pass
This would convert your input to an int value.
Conclusion
In general, the two methods presented above are simple solutions to the TypeError: ‘<‘ Not Supported Between Instances Of ‘str’ And ‘int’ major mistake. If you still have more questions, please leave them in the comment part. I wish you a gainful day with the tool Python.