Traceback (most recent call last):
File "main.py", line 17, in <module>
max_height = max(botmoves)
TypeError: '>' not supported between instances of 'tuple' and 'int'
This is the error I'm trying to find the biggest value in a list but it saying something about a ">" here is my code
from random import randint
points = 0
botmoves = [-1000]
for i in range(20):
guess = randint(0, 100)
print('Bot',"guessed the number was", guess)
print("The bot was",abs(guess-100),"off")
print("The bot gets",50 - abs(guess-100),"points")
points = 50 - abs(guess-100),"points"
botmoves.append(points)
max_height = max(botmoves) #this is where the error is
print(botmoves)
1 Answer 1
The max function needs to be able to compare values to each other, and it does that with the > operator. What the error is telling you is that there are different types of elements in the list, which cannot sensibly be compared to each other. In this case, an int and a tuple.
The reason for that is this line:
points = 50 - abs(guess-100),"points"
The ,"points" at the end makes points into a tuple, for example (37, "points"). The parentheses are optional in many cases.
Probably that's just a copy/paste mistake from the line above, and you didn't mean to put that there:
points = 50 - abs(guess-100)
botmovesareints and others aretuples.pointsvariable, whose max value you are trying to spot, is of typeList[Tuple]. You have to use a customkey=to the max function if you want to make it work for complex types. In other words, this will work if you instead use aList[int]for yourbotmovesvar.