【问题标题】:python blackjack game, loop generating new values each timepython二十一点游戏,每次循环生成新值
【发布时间】:2012-09-21 07:05:25
【问题描述】:
def deck():
    cards = range(1, 12)
    return choice(cards)

def player():
    card1 = deck()
    card2 = deck()
    hand = card1 + card2
    print card1, card2
    while hand < 21:
        choice = raw_input("Would you like to hit or stand?: ")
        print choice
        if choice == "hit":
            hand2 = hand + deck()
            print hand2     
        elif choice == "stand": 
                return hand 

大家好,

我正在尝试用 python 制作一个简单的二十一点游戏。我已经走到这一步了,我似乎被困住了。当我尝试弹奏时,它会询问我是否要击打或站立哪个好;但是,我的问题是它似乎每次都在生成新的卡值。这意味着如果我击中而不是站立,它将返回原始值而不是三张牌的新值。

您可以告诉我,我是编程新手,因此非常感谢任何帮助,我希望尽可能多地使用我自己的代码。

【问题讨论】:

  • 你真的需要制作一副 52 张牌...

标签: python if-statement python-2.7


【解决方案1】:

如果我正确理解了您的问题,那么可能 hand2 = hand + deck() 在您的 if 中造成了问题。您正在将总和分配给新值,该值甚至没有在任何地方使用。 您需要更新相同的hand。所以将上面的语句替换为:-

hand = hand + deck()

【讨论】:

    【解决方案2】:
    if choice == "hit":
          hand2 = hand + deck()
          print hand2     
    

    “命中”块修改hand2 而不是hand

    if choice == "hit":
          hand += deck()
          print hand
    

    【讨论】:

    • 愚蠢的我。谢谢,非常感谢。
    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2020-05-18
    • 2013-12-27
    • 1970-01-01
    相关资源
    最近更新 更多