Python 在玩游戏的时候,我怎样才能让它打印出1张王牌,11张杰克牌,12张皇后牌和13张国王牌

Python 在玩游戏的时候,我怎样才能让它打印出1张王牌,11张杰克牌,12张皇后牌和13张国王牌,python,random,blackjack,Python,Random,Blackjack,在python程序仍然正常运行的情况下,如何让它在玩游戏时打印ace而不是1,jack而不是11,queen而不是12,king而不是13 有没有一种方法我可以做到这一点,而不会改变太多 这是我的代码: import random dealer_cards = [] player_cards = [] while len(dealer_cards) != 2: dealer_cards.append(random.randint(1, 13)) if len(dealer_

在python程序仍然正常运行的情况下,如何让它在玩游戏时打印ace而不是1,jack而不是11,queen而不是12,king而不是13

有没有一种方法我可以做到这一点,而不会改变太多

这是我的代码:

import random 

dealer_cards = []
player_cards = []

while len(dealer_cards) != 2:
    dealer_cards.append(random.randint(1, 13))
    if len(dealer_cards) == 2:
        print('The dealer has a hidden card and', dealer_cards[1])

while len(player_cards) != 2:
    player_cards.append(random.randint(1, 11))
    if len(player_cards) == 2:
        print('You have the cards,', player_cards)

if sum(dealer_cards) == 21:
    print('The dealer has the cards,', dealer_cards)
    print('The dealer has won because he has 21!')
    exit()

if sum(player_cards) == 21 and sum(dealer_cards) == 21:
    print('draw')
    exit()

elif sum(dealer_cards) > 21:
    print('The dealer has the cards,', dealer_cards)
    print('The dealer has bust because he has over 21!')
    exit()

while sum(player_cards) < 21:
    choice = str(input('Choose twist or stick? '))
    if choice == 'twist':
        player_cards.append(random.randint(1, 11))
        print('You now have the cards,', player_cards)
    else:
        print('The dealer has the cards,', dealer_cards)
        print('You have the cards,', player_cards)
        if sum(dealer_cards) > sum(player_cards):
            print('The dealer has won!')
            break
        else:
            print('You have won!')
            break

if sum(player_cards) > 21:
    print('You have bust because you are over 21!')

elif sum(player_cards) == 21:
    print('You have won because you have 21')
随机导入
经销商卡=[]
玩家卡=[]
而len(经销商卡)!=2:
经销商卡。附加(随机。随机(1,13))
如果len(经销商卡)=2:
打印('经销商有一张隐藏卡和',经销商卡[1])
而len(玩家卡)!=2:
玩家卡。附加(随机。随机(1,11))
如果len(玩家卡)=2:
打印('你有牌',玩家卡)
如果总和(经销商卡)==21:
打印(‘经销商有卡’,‘经销商卡’)
打印(“经销商赢了,因为他有21个!”)
退出()
如果总和(玩家卡)=21,总和(庄家卡)=21:
打印('绘制')
退出()
elif金额(经销商卡)>21:
打印(‘经销商有卡’,‘经销商卡’)
打印(‘经销商因为超过21岁而破产!’
退出()
当总和(玩家卡)<21时:
choice=str(输入('选择扭曲或粘贴?'))
如果选项==“扭曲”:
玩家卡。附加(随机。随机(1,11))
打印('您现在有卡',玩家卡)
其他:
打印(‘经销商有卡’,‘经销商卡’)
打印('你有牌',玩家卡)
如果总和(庄家卡)>总和(玩家卡):
打印(“经销商赢了!”)
打破
其他:
打印(“你赢了!”)
打破
如果总和(玩家卡)>21:
打印('你因为超过21岁而半身像!')
elif总和(玩家卡)=21:
打印('你赢了,因为你有21')

您可以记录姓名并使用它提取姓名:

card_names = {
   1: 'Ace',
   11: 'Jack',
   12: 'Queen',
   13: 'King',
}

>>> n = 12
>>> print(card_names.get(n, n))
Queen
get
的第二个参数确保,如果在dict中找不到一个数字,它将按原样打印

现在,您的代码正在打印列表,这使得它更加复杂。您必须为列表中的每个元素查询该dict

您的代码:

print('You now have the cards,', player_cards)
变成:

print('You now have the cards: ', 
    ', '.join(str(card_names.get(c, c)) for c in player_cards))
这将在卡片上迭代并逐个查询dict。每次打印列表时都要这样做,这样就行了

由于每次打印列表时都必须重复该代码段,因此可以创建一个函数来避免重复:

def format_card_list(card_list):
    return ', '.join(str(card_names.get(c, c)) for c in card_list))
然后在代码中使用它:

print('You now have the cards: ', format_card_list(player_cards))
...
print('The dealer has the cards,', format_card_list(dealer_cards))
...

etc

硬编码将整数值映射到要打印的字符串的dict。如果您需要特定的详细信息,请参阅其他纸牌游戏安装。{1:“ace”,11:“jack”,…}你考虑过用
\uu str\uuu
方法制作
卡片吗?谢谢,我会试试。