-3

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

print "Welcome to BlackJack"

def run():

    import random
    from random import choice
    import sys
    money = 500

变量“金钱”根据游戏的输赢而变化。当播放再次选择播放时,我希望结束变量成为开始变量。

    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()      
4

3 回答 3

2

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

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

于 2013-01-17T15:31:52.547 回答
1

最简单的方法是将参数添加到run

def run(money):

删除线money = 500,在循环中和run第一次调用。run(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 次)并且仍然很好地考虑您的代码。

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

于 2013-01-17T15:32:20.490 回答
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:他得到了支票:))

于 2013-01-17T15:31:15.877 回答