python blackjack ACE破坏了我的代码计算
我正在编写一个21点游戏。我附加所有卡片,包括皇室和王牌以及2到10。我的问题是A。我的问题是在计算A之前要计算所有的牌,为了澄清这一点,我的意思是我要计算列表中任何A之前的所有牌,这样我可以看到总数是否大于11,如果大于11,那么只需为每个A添加1,如果不是,则添加11。到目前为止,我的代码是:python blackjack ACE破坏了我的代码计算,python,blackjack,Python,Blackjack,我正在编写一个21点游戏。我附加所有卡片,包括皇室和王牌以及2到10。我的问题是A。我的问题是在计算A之前要计算所有的牌,为了澄清这一点,我的意思是我要计算列表中任何A之前的所有牌,这样我可以看到总数是否大于11,如果大于11,那么只需为每个A添加1,如果不是,则添加11。到目前为止,我的代码是: import random def dealhand(): #This function appends 2 cards to the deck and converts royals and ac
import random
def dealhand(): #This function appends 2 cards to the deck and converts royals and aces to letters.
player_hand = []
for card in range(0,2):
card = random.randint(1,13)
if card == 1:
card = "A"
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
player_hand.append(card)
return player_hand
#this function sums the given cards to the user
def sumHand(list):
total = 0
for card in list:
card = str(card)
if card == "J" or card == "Q" or card== "K":
total+=10
continue
elif card == "2" or card == "3" or card == "4" or card == "5" or card == "6" or card == "7" or card == "8" or card == "9" or card == "10":
total += int(card)
elif card == "A":
if total<11:
total+=11
else:
total+=1
return total
随机导入
def dealhand():#此函数将两张卡追加到牌组中,并将皇家和王牌转换为字母。
玩家手=[]
对于范围(0,2)内的卡:
card=random.randint(1,13)
如果卡==1:
卡片=“A”
如果卡==11:
card=“J”
如果卡==12:
card=“Q”
如果卡==13:
card=“K”
玩家手牌追加(牌)
还手
#此函数用于向用户汇总给定的卡
def sumHand(列表):
总数=0
对于列表中的卡:
卡片=str(卡片)
如果卡片==“J”或卡片==“Q”或卡片==“K”:
总数+=10
持续
elif卡片==“2”或卡片==“3”或卡片==“4”或卡片==“5”或卡片==“6”或卡片==“7”或卡片==“8”或卡片==“9”或卡片==“10”:
总计+=整数(卡)
elif卡==“A”:
如果总数我建议总数+=1,aces+=1,然后在最后,如果需要,为每个ace添加10
关于提问的几点提示:不要发布dealhand函数,因为这完全不相关。张贴输入、输出和预期输出
def sumHand(hand):
...
hand = ['A', 'K', 'Q']
expected 21
actual 31
这是我建议的修复方法(针对此特定问题的最小更改)
def sumHand(手动):
总数=0
ACE=0
对于手中的卡:
卡片=str(卡片)
如果卡片==“J”或卡片==“Q”或卡片==“K”:
总数+=10
持续
elif卡片==“2”或卡片==“3”或卡片==“4”或卡片==“5”或卡片==“6”或卡片==“7”或卡片==“8”或卡片==“9”或卡片==“10”:
总计+=整数(卡)
elif卡==“A”:
总数+=1
ACE+=1
对于范围内的(aces):
如果total看起来像是你需要把你的卡片列表分成2张;不是-‘a’和‘a’元素,然后在其中一个元素上操作,然后在另一个元素上操作。您的解决方案有什么问题?顺便说一句:defsumhand(列表):
您真的不应该对python名称/关键字进行阴影处理。。。我的作业要求我使用变量名列表,尽管我觉得这也有点奇怪
def sumHand(hand):
total = 0
aces = 0
for card in hand:
card = str(card)
if card == "J" or card == "Q" or card== "K":
total+=10
continue
elif card == "2" or card == "3" or card == "4" or card == "5" or card == "6" or card == "7" or card == "8" or card == "9" or card == "10":
total += int(card)
elif card == "A":
total += 1
aces += 1
for _ in range(aces):
if total <= 11:
total += 10
return total