我写这个函数是为了给Python 21点游戏中的手牌“评分”(我知道A可以是1或者11,这是将来要解决的问题)。输入是元组(卡片)的列表,等级可以是2-10或A、J、K或Q。
我有时会得到一个TypeError,它在引用score = score + rank时读取TypeError: unsupported operand type(s) for +: 'int' and 'str'。我不确定为什么会有问题,因为如果到了那个地步,等级必须是int。有人看到这个问题了吗?
def analyzeHand(hand):
score = 0
for card in hand:
rank = card[0]
if (rank == 'A'):
score = score + 11
if (rank == 'K' or rank == 'Q' or rank == 'J'):
score = score + 10
else:
score = score + rank
return score发布于 2020-04-25 03:02:38
最后一个中的score = score + rank应该是
score += int(rank)因为从上下文rank是str类型,假设是"2".."9“。
https://stackoverflow.com/questions/61415604
复制相似问题