[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! 📚


import_contacts

You may also like:

Related Posts

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Python range float
    17 December

    Generate float range in Python [9 ways]

    Table of ContentsWhat is a floating-point number in Python?Why not use the simple and popular range() function?Python range float : How to generate floating-point numbers in Python?Using the numpy.linspace() method.Using the numpy.arange() method.Using list comprehension.Using generator comprehension.Using yield generator.Using a user-defined function.Using the itertools module and its functions.Using the map() function along with a lambda […]

  • Python callback function
    13 December

    Callback function in Python

    Table of ContentsWhat is Callback Function in Python?Applications of Callback Functions in PythonConclusion In python, functions are first-class objects. In other words, we can pass a function to a different function as an input argument. We can return a function from another function, and we can assign a function to a variable. In this article, […]

  • ValueError: too many values to unpack
    10 December

    [Fixed] ValueError: too many values to unpack (expected 2)

    Table of ContentsWhat is unpacking in python?What is ValueError: too many values to unpack (expected 2)?How to resolve ValueError: too many values to unpack (expected 2)?Conclusion While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the ValueError exception with the message […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python if not
    08 December

    Python If with NOT Operator

    Table of ContentsNOT Operator and IF Statement in PythonApplications of the if statement with not operator in pythonConclusion In python, we use conditional statements to implement logic in our program. For this, we use boolean expressions with if statements. In this article, we will see how we can use the if statement with the not […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.