Python Forum
TypeError: '>' not supported between instances of 'str' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: '>' not supported between instances of 'str' and 'int'
#1
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?
Reply
#2
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,060 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 8,793 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,475 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 8,796 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 19,598 Dec-06-2019, 12:40 AM
Last Post: palladium
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 10,494 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 3,969 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,055 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 7,932 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 5,549 Mar-05-2019, 09:51 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020