timmy zrewidował ten Gist 10 months ago. Przejdź do rewizji
2 files changed, 64 insertions
bugged_card.py(stworzono plik)
| @@ -0,0 +1,43 @@ | |||
| 1 | + | """ | |
| 2 | + | 撲克牌 | |
| 3 | + | """ | |
| 4 | + | ||
| 5 | + | ||
| 6 | + | class Card: # 類別 | |
| 7 | + | """ | |
| 8 | + | 撲克牌 | |
| 9 | + | """ | |
| 10 | + | ||
| 11 | + | def __init__(self, suit, number): | |
| 12 | + | """ | |
| 13 | + | 建構函式 | |
| 14 | + | """ | |
| 15 | + | self._suit = suit | |
| 16 | + | self._number = number | |
| 17 | + | ||
| 18 | + | def __repr__(self): | |
| 19 | + | return self._number + " of " + self._suit | |
| 20 | + | ||
| 21 | + | @property | |
| 22 | + | def suit(self): | |
| 23 | + | return self._suit | |
| 24 | + | ||
| 25 | + | @suit.setter | |
| 26 | + | def suit(self, suit): | |
| 27 | + | # if suit in ["hearts", "clubs", "diamonds", "Spades"]: | |
| 28 | + | if suit in ["hearts", "clubs", "diamonds", "spades"]: | |
| 29 | + | self._suit = suit | |
| 30 | + | else: | |
| 31 | + | print("That's not a suit!") | |
| 32 | + | ||
| 33 | + | @property | |
| 34 | + | def number(self): | |
| 35 | + | return self._number | |
| 36 | + | ||
| 37 | + | @number.setter | |
| 38 | + | def number(self, number): | |
| 39 | + | if number in [str(n) for n in range(2, 11)] + ["J", "Q", "K", "A"]: | |
| 40 | + | # self._number = self._number | |
| 41 | + | self._number = number | |
| 42 | + | else: | |
| 43 | + | print("That's not a valid number") | |
bugged_card_test.py(stworzono plik)
| @@ -0,0 +1,21 @@ | |||
| 1 | + | from bugged_card import Card | |
| 2 | + | ||
| 3 | + | # Create two cards | |
| 4 | + | card1 = Card("hearts", "2") | |
| 5 | + | card2 = Card("clubs", "A") | |
| 6 | + | ||
| 7 | + | # Print out their values | |
| 8 | + | print(card1) | |
| 9 | + | print(card2) | |
| 10 | + | ||
| 11 | + | # Change the suit of the first card | |
| 12 | + | card1.suit = "spades" | |
| 13 | + | ||
| 14 | + | # ASSERT 1 - check card1 is now spades | |
| 15 | + | assert card1.suit == "spades" | |
| 16 | + | ||
| 17 | + | # Change the number of card 2 | |
| 18 | + | card2.number = "2" | |
| 19 | + | ||
| 20 | + | # ASSERT 2 - card1 and card2 should now have the same number | |
| 21 | + | assert card1.number == card2.number | |