[Fixed] TypeError: ‘<' not supported between instances of 'str' and 'int'

Introduction

Problem: How to fix – TypeError: '<' not supported between instances of 'str' and 'int' in Python?

Example:

Output:

Enter a number: 20
The minimum number is: 1
Traceback (most recent call last):
File “D:/PycharmProjects/PythonErrors/rough.py”, line 3, in
print(“The minimum number: “, min(5, num, 25))
TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’

Explanation:

  • In the above example, you can see that when three integers were passed to the min function, then the program gave us the output by fetching the minimum number, i.e., 1.
  • But, when we passed a string value to the min function along with two other integers, then it resulted in a Type Error: '<' not supported between instances of 'str' and 'int'.

Hence the TypeError occurred when we compared two variables whose datatypes are completely different (a string and integer in this case.)

Before proceeding further, let us have a quick look at some of the key concepts that will help you to identify and avoid TypeErrors in the future.

💡 Python TypeError

  • TypeError is one of the most common standard Python exceptions that occur in Python. Python throws a TypeError when an operation is carried out on an incorrect/unsupported object type. 
  • For example, using the < (less than) operator on an integer and a string value will throw a TypeError.
    • This is because integers and strings are two different types of data types and cannot be compared with the help of operators used for comparisons such as <, >, >=, <=, ===, !==.

📖 Read more here: What is TypeError in Python?

📍 The following example will be used to demonstrate the solutions to our mission- critical question:- How to fix TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’?

Example

The following table lists the tenure and the corresponding Fixed Deposit rates of a certain bank.

TenureFD Interest Rates
6 months to 364 days5.25%
18 months 1 day to 1 year 364 days6.00%
1 year 1 day to 18 months5.75%
2 years to 2 years 364 days6.00%
3 years to 4 years 364 days5.65%
5 years to 10 years5.50%

In the following code, when the user selects a certain tenure the corresponding FD rates shall be displayed. Also, if tenure is greater than 1 year 1 day to 18 months, then customer is a “Prime Customer” else “Normal Customer”.

Output:

The above error occurred because if option < 4 is an unsupported operation. Let’s solve this issue using the following methods.

📌 Method 1: Using int()

Instead of accepting the user input as a string, you can ensure that the user input is an integer with the help of int() method.

Solution:

Output:

Explanation: int(value) function converts the user-input to an integer value. Thus now you can compare two values of the same data type using the < operator.

📌 Method 2: Using a Custom Function

Instead of manually typecasting the string value to an integer you can also define a function to convert it to a string.

Example:

Output:

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

Select the Tenure (1 – 6): 5
Prime Customer
Rate = 5.65%

🐰 Bonus Read: Python Typecasting

Type Casting is the process of converting a literal of one type to another type. Typecasting is also known as explicit type conversion. Python inbuilt functions like int(), float() and str() are used for typecasting.

For example:

  • int() function can accept a float or string literal as an input and return a value of int type.

Output:

Conclusion

Hence, in this article, you learned what is Type Error: '<' not supported between instances of 'str' and 'int' and the possible causes and solutions of the error.

In general, if you are getting a TypeError in your code, take a moment to verify that the data you are operating on is actually of the type you would expect it to be.

You can rectify this error by manually converting the datatype so that the comparison is performed between values of the same datatypes.

Recommended Tutorials:

Please subscribe and stay tuned for more tutorials. Happy learning! 📚

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *