Alternatives to the global statement in Python -
i'm writing simple roulette simulator, focusing on red/black betting (basically game of heads or tails).
the issue have variables being called across functions.
here code (i'm sure has other issues too, i'm focusing on one, now):
import random # defines initial amounts of money , losses money = 50 losses = 0 # starts sim def roulette_sim(): print "how want bet?" bet = int(raw_input("> ")) if bet > money: bet_too_much() else: red_or_black() # prevents 1 betting more money 1 has def bet_too_much(): print "you not have money. please bet again." raw_input("press enter continue> ") roulette_sim() # asks user select red or black, starts sim, modifies money/losses def red_or_black(): print "ok, bet %r" % (bet) print "red or black?" answer = raw_input("> ") number = random.randint(1, 2) if number == 1 , answer == "red": print "you win!" money += bet print "you have %r money" % (money) print "your losses %r" % (losses) replay() elif number == 2 , answer == "black": print "you win!" money += bet print "you have %r money" % (money) print "your losses %r" % (losses) replay() else: print "you lost!" money -= bet losses += bet print "you have %r money" % (money) print "your losses %r" % (losses) replay() # asks user whether he/she wants play again def replay(): print "do want play again?" play_again = raw_input("y/n> ") if play_again == "y": roulette_sim() else: print "ok, bye loser!" roulette_sim()
so, nameerror: global name 'bet' not defined
, avoid using global
, i'd rather not resort that. how can call variable, value assigned user in roulette_sim
, in other functions, other via using global
?
i suppose same issue apply money
variable.
i have extremely basic knowledge of python, apologise mistakes.
thanks
you can pass values arguments functions , make functions return updated values, e.g:
def roulette_sim(money, losses): print "how want bet?" bet = int(raw_input("> ")) if bet > money: return bet_too_much(money, losses) else: eturn red_or_black(money, losses, bet)
where red_or_black
instead of modifying global money
, losses
variables passes amount of lost moey replay
calls roulette_sim
updated values.
alternatively can put class , use self.money
, self.losses
, self.bet
"globals".
however may notice whole design of these functions awful. have complicated relations , use recursion, means after (probably less than) 1000 bets you'll receive runtimerror: maximum recursion depth exceeded
.
you refactor whole code use loops instead. give idea:
import random def get_bet(): bet = float('+inf') while bet > money: print("how want bet?") bet = int(raw_input("> ")) if bet > money: bet_too_much() return bet def bet_too_much(): print("you not have money. please bet again.") raw_input("press enter continue") def roulette_sim(): money = 50 losses = 0 while true: bet = get_bet() won = red_or_black(bet) money += won losses -= won
note: imagine want write different game player bets. using design it's really hacky re-use functions in new game.
with above design can freely re-use get_bet()
function asks player bet etc. give yet reason use loops instead of tightly coupling functions recursion cycles.
Comments
Post a Comment