Python黑杰克游戏变体

Python黑杰克游戏变体,python,while-loop,poker,Python,While Loop,Poker,我的Black Jack代码非常基本,但运行非常平稳,但我遇到了减速。所以我在这里。当我在While循环中调用“Hit”向我发送另一张卡时,对于每个循环,牌组都会实例化同一张卡。前2张抽牌和命中牌总是不同的,但在While循环中(当玩家说“留下”并且不想要另一张牌时,该循环设置为结束)。命中牌保持不变 import random import itertools SUITS = 'cdhs' RANKS = '23456789TJQKA' DECK = tuple(''.join(card) f

我的Black Jack代码非常基本,但运行非常平稳,但我遇到了减速。所以我在这里。当我在While循环中调用“Hit”向我发送另一张卡时,对于每个循环,牌组都会实例化同一张卡。前2张抽牌和命中牌总是不同的,但在While循环中(当玩家说“留下”并且不想要另一张牌时,该循环设置为结束)。命中牌保持不变

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")
例:手牌:Td(两颗钻石),3c(三根球杆) 命中率:7秒(7黑桃) 打7分 打7分 打7分 ...
留下来:让我们看看你做得怎么样。我需要继续While循环点击以区别任何想法。

问题是,在程序启动期间,您只生成了一次点击卡。从更改代码

    if g == 'Hit':
        print(hit)
差不多

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

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

请记住,这是有效的替换采样,因此无论您命中多少次,每张卡都将始终有相同的机会被抽取。这使得点卡变得不可能。如果你想对21点进行更真实的模拟,你可以生成一个包含N副牌的所有牌的列表,将其洗牌(random.shuffle可以做到这一点),然后在下一张牌上保留索引,或者使用pop从列表中移除并获得一张牌。谢谢,帮了大忙+感谢你的建议。shuffle(),我不知道