UnboundLocalError at Cho_han game

I am trying to solve the Cho_han game by using codes below,

def cho_han(guess, bets):
if bets <= 0:
print(“Your bet should be above 0.”)
return 0
print(“Let’s play a game!”)
result_1 = random.randint(1, 6)
result_2 = random.randint(1, 6)
All = result_1 + result_2
print("The sum of the two dice is " + str(All))

if guess == “Odd” and All % 2 == 1:
print(“You won " + str(bets) + " dollars!”)
return bets

elif guess == “Even” and All % 2 == 0:
print(“You won " + str(bets) + " dollars!”)
return bets

print(“You lost " + str(bets) + " dollars!”)
return -bets

However, I am running into UnboundlocalError when I try to call the function. I am thinking to add “global” to solve this issue but don’t know which line should I add it in. Or do I have to a new variable to avoid this error?


better if you pass in an amount as an argument and return a new value, or pass in some form of container instead

consider if you were making a casino and needed to track the money of a hundred different people, and maybe there are different games as well so this function wouldn’t be the only one adding and removing money, and people are coming and leaving all the time and … these problems are solved if the player says: hey, this is how much money I have, how much will I have after the game? … so long as they are only playing one game simultaneously, that is. you could also instead say how much gain/loss there is, then you wouldn’t need to touch any wallets at all, the caller would deal with it


Thanks for helping him out…

not happening

I am sorry. I am afraid I do not understand what you mean…

I was just trying to be kind if that is what you were wondering