Python - 二十一点程序

时间:2013-01-17 15:27:40

标签: python

我正在用python创建一个黑杰克游戏。当函数循环时,如何将结束货币变量移动到函数顶部?

print "Welcome to BlackJack"

def run():

    import random
    from random import choice
    import sys
    money = 500

变量'money'的变化取决于游戏是赢还是输。当所选播放再次播放时,我希望结束变量成为开始变量。

    raw_input("Press <ENTER> To Begin")
    print "You have $",money,"in your bank."
    bet = raw_input("How much would you like to bet?")

    b = int(bet)

    cards = [1,2,3,4,5,6,7,8,9,10,10,10,10]*4

    c1 = choice(cards)
    cards.remove(c1) 

    c2 = choice(cards)
    cards.remove(c2)

    psum = c1 + c2

    print "You were dealt a",c1,"and a",c2,"for a sum of",psum,
    print "\n"
    hs = " "

    while psum < 21 and "s" not in hs:
        hs = raw_input("Hit or Stand (h or s): ").lower()
        if "h" in hs:
            c3 = choice(cards)
            cards.remove(c3)
            psum = psum + c3
            print "You were dealt a",c3,"for a sum of",psum,
            print "\n"
        elif "s" in hs:
            print "Your final sum is",psum,

    print "\n"

    if psum > 21:
        print "Bust!" "\n" "You lose." "\n"
        money = money - b
        print "You now have $",money,"in your bank."
    elif psum == 21:
        print "You got a BlackJack!" "\n" "You win!" "\n"
        money = money + b
        print "You now have $",money,"in your bank."   
    else:
        print "Dealer's turn"

    if psum < 21:   
        c4 = choice(cards)
        cards.remove(c4) 

        c5 = choice(cards)
        cards.remove(c5)

        dsum = c4 + c5

        while dsum < 17:
            c6 = choice(cards)
            cards.remove(c6)
            dsum = dsum + c6

        if dsum > 21:
            print "Dealer's final sum is",dsum,"\n"
            print "Dealer bust! You win!" "\n"
            money = money + b
            print "You now have $",money,"in your bank."
        elif dsum < psum:
            print "Dealer's final sum is",dsum,"\n"
            print "You win!" "\n"
            money = money + b
            print "You now have $",money,"in your bank."
        elif dsum == psum:
            print "Dealer's final sum is",dsum,"\n" 
            print "Draw." "\n"
            print "You have $",money,"in your bank."
        else:
            print "Dealer's sum is",dsum,"\n"
            print "You lose." "\n"
            money = money - b
            print "You now have $",money,"in your bank."


    yn = raw_input("Would you like to play again? (y or n): ")

    if "y" in yn:
        print "\n" * 5
        run()
    else:
        print "\n" "Your total winnings is $",money,
        sys.exit()          

run()      

3 个答案:

答案 0 :(得分:2)

每次玩家选择再次玩时,不应该调用run(),而应该将所有代码放在循环中,当玩家选择“否”时,该循环会中断。这样money变量将继续保持其值。

编辑:将该代码移动到单独的方法(例如,清晰且可维护的代码)肯定是有利的。 deal_a_hand(),并且每次都将money变量传递给它(您可能需要方法return money),但最好从主方法中的循环调用它而不是使用它不必要的递归。通常,您不希望方法调用自身,除非它使程序更有效或更容易编写,即使这样,您也必须考虑递归的深度。

答案 1 :(得分:1)

最简单的方法是向run添加一个参数:

def run(money):

删除第money = 500行,在循环中调用runrun(money),第一次调用run(500)

我建议从run删除“播放另一轮”逻辑

def run_single_hand(money):
    # <code to run hand, change value of money>
    return money

def play_hands():
     starting_money = 500
     money = starting_money
     money = run_single_hand(money)
     while True:
         # <code to ask if they would like to play again
         if again:
             run_single_hand(money)
         else:
             print 'thank you, you made a profit of %d' % money - starting_money
             break

因为这可以避免递归问题(这是我建议的第一种方式,最终会在堆栈上对run进行N次调用)并且仍能很好地考虑您的代码。

例如,你可以修改它来做扑克替换run_single_hand。对于这个例子来说,这似乎微不足道,但对于更复杂的项目来说,这是一个很好的代码模式。

答案 2 :(得分:0)

像这样定义你的功能:

def run(startingFunds = None):

    <brilliant code>

    money = 500 if startingFunds is None else startingFunds

    <brilliant code>

    if "y" in yn:
        print "\n" * 5
        run(money)

第二个想法,做一下iamnotmaynard建议并在它周围放一个while循环。但我仍然将startingFunds作为函数的参数。

(PS:他得到了支票:))