. Advertisement .
..3..
. Advertisement .
..4..
This guide will give clear examples of how to fix TypeError: < not supported between instances of str and int. Overall, the best way is to use the Int() function? Why so? Check our guides for explanations.
Why Does It Occur?
First, knowing something or two about integers and strings is important. It is not possible to use comparison operators to compare them. Why? Integers and strings are two different data types. And also, Python is a recent example of a statically typed language. Thus, for any value comparison, manual modifications are a must.
Suppose we want to transform a string into an integer. As we established, manual conversion is our only choice for this comparison to work out.
But what does that relate to the Typeerror: ‘< ‘not supported between instances of ‘str’ and ‘int’? These errors will pop up once you try to perform “greater than” comparisons on an integer and a string. Be careful during this process, as this error is very common and can disrupt your program anytime.
Pay close attention to all comparison operators. They include equal to or less than (=), less than (), or greater than (>=). All of them might be the culprit behind this error!
Want to see a clear example of that? Then let’s look at the illustration below:
Example 1:
Let’s say you want to measure the students’ letter grades on one test. First, request the user to enter one numerical grade. Later, we will convert it into a letter grade via input () statements like this:
numerical_grade = input(“What grade did the student earn? “)
And then, how can we identify the students’ numerical grades? It’s time to turn to the “if” statements. Let’s look at these codes:
Example 1 (Codes):
if numerical_grade > 80:
letter = “A”
elif numerical_grade > 70:
letter = “B”
elif numerical_grade > 60:
letter = “C”
elif numerical_grade > 50:
letter = “D”
else:
letter = “F”
print(letter)
The system compares the numerical variables to a number set. Our codes will measure whether this numerical grade is greater than 80. The “letter” value turns into “A” if the answer is yes. But if the answer is No, the code will inspect the succeeding “elif” statement.
Also, the codes will set “letter” values to “F” once all your “elif” and “if” lines translate into False.
We will publish the pupil’s grades at the program’s end. Run the codes; the output below might be surprising (or not!)
Example 1 (Error):
What grade did the student earn? 64
Traceback (most recent call last):
File "main.py", line 3, in <module>
if numerical_grade > 80:
Typeerror: '< 'not supported between instances of 'str' and 'int'
The error implies that our codes fail somewhere during the process. The program asks us to insert a pupil’s grade. When the codes try to perform an initial comparison, the error takes place.
How to Fix The TypeError: < Not Supported Between Instances of Str and Int
Our input() method will return a string, which means the code attempts to make a comparison between the string (“numerical_grade) and integers. So, to solve the issue, it is a must to convert “numerical_grade” to integer before inserting any comparison into the code.
Example 1 (Solution):
numerical_grade = int(input("What grade did the student earn? "))
Our int() method now transforms the inserted values into an integer. Let’s run the codes one more time:
Example 1 (Output):
What grade did the student earn? 64
C
Our solutions have worked successfully!
Let’s look at Example 2, which is similar to Example 1:
Example 2:
print("""
Tenure FD
1. 6 months to 364 days 5.25%
2. 18 months 1 day to 1 year 364 days 6.00%
3. 1 year 1 day to 18 months 5.75%
4. 2 years to 2 years 364 days 6.00%
5. 3 years to 4 years 364 days 5.65%
6. 5 years to 10 years 5.50%
""")
option = input("Select the Tenure (1 - 6): ")
if option < 4:
print("Normal Customer")
if option == 1:
print("Rate = ", 5.25)
if option == 2:
print("Rate = ", 6.00)
if option == 3:
print("Rate = ", 5.75)
else:
print("Prime Customer")
if option == 4:
print("Rate = ", 6.00)
if option == 5:
print("Rate = ", 5.65)
if option == 6:
print("Rate = ", 5.50)
The error will appear once we print out these codes. What should we do then?
......... ADVERTISEMENT .........
..8..
The best way is to use the int() function. Why? The reason is simple. The int() function can convert your user input into an integer. Comparing two values from similar data types with the < operators is possible now.
Example 2 (Solution):
print("""
1. 6 months to 364 days
2. 18 months 1 day to 1 year 364 days
3. 1 year 1 day to 18 months
4. 2 years to 2 years 364 days
5. 3 years to 4 years 364 days
6. 5 years to 10 years
""")
option = int(input("Select the Tenure (1 - 6): "))
if option < 4:
print("Normal Customer")
if option == 1:
print("Rate = 5.25%")
if option == 2:
print("Rate = 6.00%")
if option == 3:
print("Rate = 5.75%")
else:
print("Prime Customer")
if option == 4:
print("Rate = 6.00%")
if option == 5:
print("Rate = 5.65%")
if option == 6:
print("Rate = 5.50%")
Conclusion
Our guides have given some examples of fixing the TypeError: < not supported between instances of str and int. Feel free to browse ITtutoria websites for solutions to similar errors (such as “cannot read property ‘0’ of undefined error“). You can also write to us for more support.
Leave a comment