Home > OS >  Python UNO Game Errors
Python UNO Game Errors

Time:12-27

I am creating a UNO game. I think I have a problem in class Uno, def playGame and playTurn. I get the following errors. In def playGame, I tried to check if the deck or any hands of a player is empty. If it is then the game is over and we have a winner. In playTurn I checked if there is a playable card in a player's hand by sending the card to the function canPlay. If there is a playable card then player plays the card and I remove the card from hand.

Traceback (most recent call last): File "c:\Users\yaman\OneDrive\Desktop\uno_game.py", line 150, in main()

File "c:\Users\yaman\OneDrive\Desktop\uno_game.py", line 147, in main my_game.playGame()

File "c:\Users\yaman\OneDrive\Desktop\uno_game.py", line 92, in playGame self.lastPlayedCard=random.choice(self.hand1)

File "C:\Python39\lib\random.py", line 347, in choice return seq[self._randbelow(len(seq))] TypeError: object of type 'CollectionOfUnoCards' has no len()

CodePudding user response:

You tried to calculate size of CollectionOfUnoCards and program fails. In python you should declare custom __len__ method to invoke len function on custom classes.

Try to add __len__ to CollectionOfUnoCards like this

class CollectionOfUnoCards:
    def __len__(self):
        return len(self.cardlist)

Also you have errors here

  • random.choice(self.hand1) -> random.choice(self.hand1.cardlist), because you can invoke random.choice on list.
  • for card in self.hand1: -> for card in self.hand1.cardlist:
  • self.hand1.remove(play) -> self.hand1.cardlist.remove(play)
  • Related