0
oopに新しく追加されました。私はget_valueメソッドを完了しようとしています。dictのキーとobjectを比較し、TypeError: 'Card'オブジェクトは反復不可能です
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
class Hand:
def __init__(self):
self.cards = [] # create Hand object
def __str__(self):
self.string = ''
for card in self.cards:
self.string += card.__str__()
return 'Hand contains ' + self.string # return a string representation of a hand
def add_card(self, card):
self.cards.append(card) # add a card object to a hand
def get_value(self):
self.x = []
for key, value in VALUES2.items():
for card in self.cards: #also tried for card in h.cards if h = Hand()
test2 = map(lambda each:each.strip("C"), card) #line 57
if key in test2:
self.x.append(value)
hand_value = sum(self.x)
return hand_value
私が実行すると、次のようになります。57行目:TypeError: 'Card'オブジェクトは反復可能ではありません。
c2 = Card("C", "2")
test_hand = Hand()
test_hand.add_card(c2)
print test_hand
は
Hand contains C2
おかげで、物事のコレクションに関数を適用することにより、