Python-blackjack程序
我正在用python创建一个black jack游戏。当函数循环时,如何将结束货币变量移到函数顶部Python-blackjack程序,python,Python,我正在用python创建一个black jack游戏。当函数循环时,如何将结束货币变量移到函数顶部 print "Welcome to BlackJack" def run(): import random from random import choice import sys money = 500 变量“钱”根据游戏的输赢而变化。我希望在播放再次选择播放时,结束变量成为开始变量 raw_input("Press <ENTER> To
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()
如下定义您的函数:
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的建议去做,并围绕它做一个循环。但我仍然会把startingFunds作为函数的参数
PS:他得到检查:而不是每次玩家选择再次玩时调用run,你应该将所有代码放入一个循环中,当玩家选择no时,该循环将中断。这样,money变量将继续保持其值
编辑:对于干净且可维护的代码来说,将代码移动到一个单独的方法(例如,deal_a_hand)中,并在每次需要该方法时将money变量传递给它,然后返回money,这无疑是有利的,但最好从主方法中的循环调用它,而不是使用不必要的递归。一般来说,除非它使程序更有效或更容易编写,否则不需要调用自己的方法,即使这样,也必须考虑递归的深度。 最简单的是添加一个参数来运行: 删除行money=500,在循环中调用runasrunmoney并第一次运行500 我建议从运行中删除“玩另一轮”逻辑 因为这样可以避免递归问题,所以我建议的第一种方法最终将在堆栈上运行N个调用,并且仍然可以很好地考虑代码
例如,你可以修改它来代替单手运行扑克。对于本例来说,这似乎无关紧要,但对于更复杂的项目来说,这是一种很好的代码模式。除了美观之外,还有其他原因吗?循环的一个优点是每次调用run时,它都会在堆栈上放入另一轮变量,并进行不必要的导入,虽然循环只保留一组变量。@BenDundee是的,函数调用堆栈不会通过反复运行调用自身来不断构建。啊,我明白了……这是一个更好的设计。这更好,因为它更容易控制玩家开始时的钱数。
def run(money):
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