我正在创建二十一点模拟程序。我有两个班级,分别是手牌和策略。在策略课程中,我有一个玩功能,应该玩一手牌。我得到TypeError'NoneType'对象是不可迭代的。我的代码如下。我究竟做错了什么?我该如何解决?
import random
import sys
import csv
from collections import namedtuple
from collections import defaultdict
Stand = namedtuple('Stand', 'stand total')
class Hand:
"""Class that encapsulates a blackjack hand."""
def __init__(self, cards=None):
super(Hand, self).__init__()
self.cards = cards
self.total, self.soft_ace_count = self.score()
def __str__(self):
return str(self.cards)
def get_card():
return random.randint(1, 13)
def add_card(self):
self.cards.append(get_card())
self.score()
def is_blackjack(self):
if self.total == 21 and self.soft_ace_count == 1 and 10 in self.cards or 11 in self.cards or 12 in self.cards or 13 in self.cards:
if 10 in self.cards or 11 in self.cards or 12 in self.cards or 13 in self.cards:
return True
return False
def is_bust(self):
return self.total > 21
def score(self):
self.total = 0
self.soft_ace_count = 0
for card in self.cards:
if card == 1:
self.total+=11
self.soft_ace_count+=1
elif card == 11 or card == 12 or card == 13:
self.total+=10
else:
self.total+=card
for x in self.cards:
if self.total > 21 and self.soft_ace_count == 1:
if x == 1:
self.total-=10
self.soft_ace_count-=1
return (self.total, self.soft_ace_count)
class Strategy:
"""docstring for Strategy."""
def __init__(self, stand_on_value, stand_on_soft):
super(Strategy, self).__init__()
self.stand_on_value = stand_on_value
self.stand_on_soft = stand_on_soft
def stand(self, hand):
total, soft_ace_count = hand.score()
if total < self.stand_on_value:
return False
if total > self.stand_on_value:
return True
if soft_ace_count > 0 and not self.stand_on_soft:
return False
else:
return True
def play(self):
h = Hand(self.cards)
while not self.stand(h):
h.self.cards.append(get_card())
self.stand(h.self.cards)
return h.self.cards