Python 黑杰克游戏变体

Python Black Jack game variant

我的 Black Jack 代码非常基础,但是 运行 非常顺利,但是我 运行 遇到了减速带。所以我在这里。当我调用 "Hit" 在我的 While 循环中向我发送另一张卡片时,对于每个循环,DECK 都会实例化同一张卡片。抽取的前 2 张和 Hit 卡总是不同的,但在 While 循环中(设置为当玩家说 "stay" 并且不想要另一张牌时结束。)Hit 卡保持不变。

import random
import itertools
SUITS = 'cdhs'
RANKS = '23456789TJQKA'
DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS))
hand = random.sample(DECK, 2)
hit = random.sample(DECK, 1)

print("Welcome to Jackpot Guesser! ")
c = input("Would you like to play Black Jack or play Slots? ")
if c == 'Black Jack':
    print()
    print("Welcome to Black Jack! Here are the rules: ")
    print("Black Jack is a game of whit, skill with a bit of Luck. You will start with 2 card and try to achieve a total of 21 points. \n Each cards worth is equal to its number, face cards are worth 10 and the Ace can be 1 or 11 points. You decide. \n You can decide to -Stay- and take the points you have or -Hit- to get another card. If you get 21 its Black Jack and you win. \n If no one gets 21, the highest points win, but if you go over 21 you -Bomb- and lose everything. \n Becarful not to Bomb and goodluck out there! Remember, you got to know when to Hit, when to Stay and when to walk away! \n")
    print(hand)
    print()

    g = 'swag'
    while g != 'Stay':
        g = input(("What would you like to do, Stay or Hit: "))
        if g == 'Hit':
            print(hit)
        elif g == 'Stay':
            print("Lets see how you did!")
        else:
            print("test3")
elif c == 'Slots':
          print("test")
else:
    print("test2")

EX:手牌:Td(两颗方块),3c(3 梅花) 命中:7s(7个黑桃) 打7秒 打7秒 打7秒 ... 留下来:让我们看看你是怎么做的。我需要持续的 While 循环 Hits 来区分,任何想法。

问题是您只在程序启动期间生成一次命中卡。从

更改您的代码
    if g == 'Hit':
        print(hit)

类似于

    if g == 'Hit':
        hit = random.sample(DECK, 1)
        print(hit)

将使它在每次命中时输出不同的卡片。