Solving – TypeError: ‘<' not supported between instances of 'str' and 'int'

Solving – TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’ is a Python error which occurs when you are trying to do numerical operations on strings.

In this Blog post I am going to try and solve this issue while explaining it. we could not learn about solutions that worked for other people who had the same issue, but the solution I proposed is enough.

Explaining TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’

Before we solve this error, let us try to replicate a scenario where this takes place. This error will occur in any python script where you have made the mistake of trying to do numerical operations on non numerical objects like strings.

This is how the message of the error looks like

                                                                       #
TypeError: '<=' not supported between instances of 'str' and 'int'
                                                                       #

Bellow, I will present the solution that worked for me, if you have the same error, follow along.

The Solution : Use int

First, we should understand that in Python input() takes the input in form of strings by default.

So, instead of using input() directly like in the line bellow

                                                                             #
choice = input('enter the name of the fighter who you think will win')
                                                                             #

You should wrap it with int(), in this case we will replace the code above with this line of code.

                                                                             #
choice = int(input('enter the name of the fighter who you think will win'))
                                                                             #

This way you are using the correct method, now you can do your numerical operations without having this error ruin your day.

As a result the entire code without the declarations will look like this

                                                                             #
    while(choice >= 0 and choice <70):
    choice = input('enter the name of the fighter who you think will win')
    if (0 < choice <=71): fighter[choice +1] += 1;index +=1
    else:
    print('error, try again')
                                                                             #

If this article has been useful in your personal experience, consider donating to our Kofi account, there is a Huge red button at the top of this page.

Summing-up

This is it, it is the end of our article, I hope I helped you solve the error TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’.

Keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/