'What is causing the "is not supported between instances" inside my code?

So I know this code is a mess. I am trying to get the base code without importing any other libraries other than random.

def deckToShuffle(name):
  print(f'''{'-'*40}
SHUFFLE {name.upper()}
{'-'*40}''')
def getKey(val,dict):
    for key, value in dict.items():
         if val == value:
             return key
 
    return "key doesn't exist"
class JOKER:
  def __init__(self, rank):
    self.rank = rank
    self.name = 'Joker'
  def printCard(self):
    print(f'{self.rank}')
class CARD:
  def __init__(self, suit, rank):
    self.name = rank + ' of ' + suit
    self.suit = suit
    self.rank = rank
  def printCard(self):
    print(f'{self.rank} of {self.suit}')
class DECK:
  def __init__(self, name, jokers = False):
    deck = []
    self.name = name
    suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
    ranks = ['Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace']
    self.suits = suits
    self.ranks = ranks
    self.deck = deck
    self.jokers = jokers
    for suit in self.suits:
      for rank in self.ranks:
        self.deck.append(CARD(suit , rank))
    if self.jokers:
      self.deck.append(JOKER('Joker'))
      self.deck.append(JOKER('Joker'))
  def printDeck(self):
    for card in self.deck:
      card.printCard()
  def shuffle(self):
    random.shuffle(self.deck)
    deckToShuffle(self.name)
  def randomSplit(self,num):
    splitToDeck = 1
    random.shuffle(self.deck)
    dct = {}
    for n in range(1,num+1):
      dct['pl_%s' % n] = []
    while len(self.deck) != 36:
      if splitToDeck == (num+1):
        splitToDeck = 1
      card = self.deck.pop(-1)
      d = dct['pl_%s' % splitToDeck]
      d.append(card)
      splitToDeck += 1
    self.dct = dct
  def printHand(self, hand):
    print('='*40)
    print(f">>> Player {hand}'s Hand")
    for card in self.dct['pl_%s' % hand]:
        print(card.name)
  def compareTopCard(self):
    top = {}
    winner = []
    for cards in self.dct:
      top[cards] = []
      t = self.dct[cards]
      if t[0].rank == 'Joker':
        t = 100
      else:
        t = self.ranks.index(t[0].rank)
      top[cards].append(t)
      print(top)
    for tops in top:
      if winner == []:
        winner = top[tops]
      elif top[tops] == [100]:
        winner = [100]
      elif top[tops] > winner:
        winner = top[tops]
      elif top[tops] == winner:
        winner = [100]
    if winner == [100]:
      print ("WAR")
    else:
      player = getKey(winner,top)
      playerNum = player[3:]
      print (playerNum)
      
warDeck = DECK('War Deck', True)
warDeck.randomSplit(10)
#warDeck.printHand(1)
#warDeck.printHand(2)
print('='*40)
warDeck.compareTopCard()

and here is the error I get

Traceback (most recent call last):
  File "main.py", line 99, in <module>
    warDeck.compareTopCard()
  File "main.py", line 83, in compareTopCard
    elif top[tops] > winner:
TypeError: '>' not supported between instances of 'list' and 'int'

I thought I didn't have any list/int instances that would cause this error.



Solution 1:[1]

elif top[tops] > winner:
TypeError: '>' not supported between instances of 'list' and 'int'

So, what the Traceback is saying is that top[tops] is a list, while winner is an int.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FLAK-ZOSO