Pythonでブラックジャックゲームを作成しています。関数がループするときに、終了の money 変数を関数の先頭に移動するにはどうすればよいですか?
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()