Problem with the function remove in a class function

10 hours ago 1
ARTICLE AD BOX

.remove() uses Carta.__eq__ to check which card to remove and there are two problems

it has to compare also self.palo == other.palo

bool(1) gives True and bool(-1) also gives True.
bool(0) gives False. But better use True/False instead of 1/-1/0

def __eq__(self, other): if (self.valor == other.valor) and (self.palo == other.palo): return True else: return False

But you can write it shorter (because == and and already gives True/False)

def __eq__(self, other): return (self.valor == other.valor) and (self.palo == other.palo)

And now self.cartas.remove(carta) works correctly.


By the way:

In other functions you should also use True/False instead of 1/-1

As @JonSG noticed there is mistake in __contains__ - it has to be self.cartas instead of self.items

@JonSG also points out that __lt__() and __gt__() should have inverted > and < but you have exactly the same code in both.

I also don't know why __lt__() and __gt__() have if self.valor == 1 ... and if other.valor == 1.

Shorter version for mezclar

def mezclar(self): random.shuffle(self.cards)

My version after other changes:

import random class Card: # fmt: off all_colors = ["Clubs", "Diamonds", "Hearts", "Spades"] all_values = ["nothing", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] # fmt: on def __init__(self, color=0, value=0): self.color = color self.value = value def __str__(self): return self.all_values[self.value] + " of " + self.all_colors[self.color] def __lt__(self, other): if self.color > other.color: return True if self.color < other.color: return False # the same color return self.value < other.value def __gt__(self, other): if self.color < other.color: return True if self.color > other.color: return False # the same color return self.value > other.value def __eq__(self, other): return (self.value == other.value) and (self.color == other.color) def __le__(self, other): return (self < other) or (self == other) def __ge__(self, other): return (self > other) or (self == other) class Deck: def __init__(self): self.cards = [] for color in range(4): for value in range(1, 14): self.cards.append(Card(color, value)) def display(self): # for card in self.cards: # print(card) print(self) def __str__(self): s = "" for card in self.cards: s += f"{card}\n" return s def shuffle(self): random.shuffle(self.cards) def __contains__(self, item): return item in self.cards def remove(self, card): if card in self.cards: self.cards.remove(card) # --- main --- deck = Deck() # print(deck) card1 = Card(1, 13) print(f"card1: '{card1}'") print(f"deck contains '{card1}':", card1 in deck) print(f"removing ... '{card1}'") deck.remove(card1) print(f"deck contains '{card1}':", card1 in deck) # print(deck) # deck.shuffle() # print(deck) print(f"'{card1}' and '{card1}'") print("== :", card1 == card1) print("> :", card1 > card1) print("< :", card1 < card1) print(">= :", card1 >= card1) print("<= :", card1 <= card1) card2 = Card(3, 10) print(f"card2: '{card2}'") print(f"'{card1}' and '{card2}'") print("== :", card1 == card2) print("> :", card1 > card2) print("< :", card1 < card2) print(">= :", card1 >= card2) print("<= :", card1 <= card2) # deck.display() card1: 'King of Diamonds' deck contains 'King of Diamonds': True removing ... 'King of Diamonds' deck contains 'King of Diamonds': False 'King of Diamonds' and 'King of Diamonds' == : True > : False < : False >= : True <= : True card2: '10 of Spades' 'King of Diamonds' and '10 of Spades' == : False > : True < : False >= : True <= : False
Read Entire Article