“TypeError: ‘>’ not supported between instances of ‘str’ and ‘int'” in Python

Python can encounter errors while executing code. One such error is the TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’.

TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ usually occurs, if you try to compare a string datatype value (str) with an integer datatype value (int). To fix, We need to convert the value type to an integer before using the comparison operation

Understanding the TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ Error

The ‘>’ operator is used to compare two values and return a Boolean value (True or False). However, the ‘>’ operator only works with numeric values such as integers or floats. When you try to use this operator with a string and an integer, Python raises a TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’.

ERROR Message:

Traceback (most recent call last):
  File "main.py", line 25, in <module>
    if age > 18:
TypeError: '>' not supported between instances of 'str' and 'int'

Quick Example to replicate the error:

  • Getting “age” as input from the user
  • Then, check if the age is over 18 years
  • If yes, Print the age
  • Else, End the program
age = input("How old are you? ")

if age > 18:

    print(f"You can drive at age {age}.")

Output:

How old are you? 18
Traceback (most recent call last):
  File "main.py", line 25, in <module>
    if age > 18:
TypeError: '>' not supported between instances of 'str' and 'int'

This code will raise the TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ because the ‘age’ variable is a string and not an integer.

How to Debug and fix this issue:

Debug:

  • From the error message, We can understand there is an issue in line 25 ( if age > 18:)
  • It is complaining, that we cannot use a comparison operator with 2 different datatypes in this case ‘str’ and ‘int’

Modified code to find the type:

Let’s use the “type()” built-in function to get the actual data type of the variable age

age = input("How old are you? ")
#printing the type of the variable
print(type(age))
if age > 18:
  print("You can drive at age " + age)

Output:

How old are you? 10
<class 'str'>
Traceback (most recent call last):
  File "main.py", line 26, in <module>
    if age > 18:
TypeError: '>' not supported between instances of 'str' and 'int'

<class ‘str’>

Now, We can see the variable”age” is in the String datatype, Due to that, When comparing the string with integer “18”, We are getting this issue,

You might be wondering, How the variable age is declared as a string 🙂

This is a default behavior of the input() function, Whenever you are getting input from the user using the input() function it will be declared as string datatype,

Any type of value like integer or float values will be converted to string type when using input()

Fix the Code

To fix the TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ error, you need to convert the string value to an integer before using the ‘>’ operator. You can do this using the int() function.

Let’s modify the code to fix the issue

age = int(input("How old are you? "))
print(type(age))
if age > 18:
  print (age)
How old are you? 20
<class 'int'>
20

Click here to validate the code in online editor

In this example, we use the int() function to convert the ‘age’ string to an integer before comparing it to 18 using the ‘>’ operator.

Conclusion:

The TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ error occurs when you try to use the ‘>’ operator to compare a string and an integer value. To fix this error, you need to convert the string value to an integer using the int() function. Alternatively, you can also convert the integer value to a string using the str() function. By following these simple steps, you can easily fix this error and ensure that your Python code runs smoothly.

Good Luck with your Learning!!

Similar Posts