Python Black Jack 游戏变种

标签 python while-loop poker

我的 Black Jack 代码非常基本,但运行得相当顺利,但我遇到了减速带。所以我在这里。当我在 While 循环中调用“Hit”向我发送另一张卡时,对于每个循环,DECK 都会实例化同一张卡。前 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")

EX:手牌:Td(两颗钻石),3c(3 梅花) 命中:7秒(7黑桃) 击中7秒 击中7秒 击中7秒 ... 留下来:让我们看看你做得怎么样。我需要持续的 While 循环点击来不同,任何想法。

最佳答案

问题是您只在程序启动期间生成一次命中卡。更改您的代码

    if g == 'Hit':
        print(hit)

类似于

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

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

关于Python Black Jack 游戏变种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623407/

相关文章:

c# - 设计模式 : Parsing similar, 但文本文件中的模式不同

Python 导入模块但启动时无法识别它?

python - 如何在 psycopg2 查询中返回 json?

python - 查找 Redis 中值最高的前 N ​​个键

python - 将yield与python中的if/else循环结合起来

python - 如何使用 while 循环摆脱空字符串? (Python)

python - 一组玩家的所有可能的纸牌/扑克牌组合

Python:类型错误: 'NoneType' 对象不可下标

java - 在 while 循环中使用 switch 语句

python - 如何加快我的 Python 扑克手与手牌赢率计算器