我跟着如何像计算机科学家一样思考,我遇到了这个错误。这是代码:
class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank def __str__(self): return (self.rankList[self.rank] + " of " + self.suitList[self.suit]) def __cmp__(self, other): if self.suit == 1: return 1 if self.suit > other.suit: return 1 if self.suit < other.suit: return -1 if self.rank > other.suit: return 1 if self.rank < other.suit: return -1 return 0
class Deck: def init(self): self.cards = [Card(suit, rank) for suit in range(4) for rank in range(1,14)]
def __str__(self): s = "" for i in range(len(self.cards)): s = s + " "*i + str(self.cards[i]) + "\n" return s def shuffle(self): import random nCards = len(self.cards) for i in range(nCards): j = random.randrange(i, nCards) self.cards[i], self.cards[j] = self.cards[j], self.cards[i] def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: return False def popCard(self): return self.cards.pop() def isEmpty(self): return (len(self.cards) == 0) def deal(self, hands, nCards = 999): nHands = len(hands) for i in range(nCards): if self.isEmpty(): break card = self.popCard() hand = hands[i % nHands] hand.addCard(card)
class Hand(Deck): def init(self, name=""): self.cards = [] self.name = name
def addCard(self, card): self.cards.append(card) def __str__(self): s = "Hand " + self.name if self.isEmpty(): return s + " is empty\n" else: return s + " contains\n" + Deck.__str__(self)
class CardGame: def init(self): self.deck = Deck() self.deck.shuffle()
class OldMaidHand(Hand): def removeMatches(self): count = 0 originalCards = self.cards[:] for card in originalCards: match = Card(3 - card.suit, card.rank) if match in self.cards: self.cards.remove(card) self.cards.remove(match) print "Hand %s: %s matches %s" % (self.name, card, match) count = count + 1 return count
我得到的错误:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
hand.removeMatches()
File "C:\Users\Lin\projects\thinkcs\cardgame.py", line 89, in removeMatches
self.cards.remove(match)
ValueError: list.remove(x): x not in list
答案 0 :(得分:0)
我在 public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name ?? GetType().Name, config);
}
类的__cmp__()
函数中看到了一个问题 -
Card
你确定要比较等级和套装吗?也许这就是造成这个问题的原因?