Python Forum

Full Version: TypeError: '>' not supported between instances of 'str' and 'int'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having some trouble with this code:

def is_hot (N):
if N<2:
return ("cold")
elif is_hot(N/2) > 1 or is_hot(N-1) > 1:
return ("hot")
n = float(input(("Enter Number: ")))
is_hot(n)

as I keep getting the following error: TypeError: '>' not supported between instances of 'str' and 'int'

Does anyone know why this could be the case?
First, when you post some code, put in between [pyt[i][/i]hon] and [/pyt[i][/i]hon] tags, so it keeps proper format which is very important in python, as you know!
So, let's have a look:
def is_hot (N):
    if N<2:
        return ("cold")
    elif is_hot(N/2) > 1 or is_hot(N-1) > 1:
        return ("hot")


n = float(input(("Enter Number: ")))
print(is_hot(n))
If you input 1, the program runs OK, good news, but is you input 2, then you have this strange error:
elif is_hot(N/2) > 1 or is_hot(N-1) > 1:
TypeError: unorderable types: str() > int()
If we go step by step:
0. We input the value 2
1. We execute is_hot(2)
2. As N is not strictly less than 2, we branch to the elseif
3. We evaluate is_hot(N/2) --> is_hot(1)
4. We execute is_hot(1)
5. This time, as N is strictly less than 2, we return "cold"
6. So, the "is_hot(N/2)" we are evaluating at step 3 has the value of "cold"
7. We evaluate "cold" > 1 --> BINGO!

As I assume this is a homework, I let you find the solution!