'Variable in function python [closed]
i have a little problem with my code. How can i make global variable in my code and functions? Example in my project ( it's simple game ) i want have a wallet - so its must be a global to earn and lose gold. In this code player is in minigame and can lose or earn more money but I would like this wallet to be variable throughout the file and react along with the minigame and other things in the code. How to do it correctly?
leave_action = 'n'
print("Welcome in dice poker.")
while leave_action.lower() != 'y':
wallet = 5
bounty_from_dice = 0
if wallet == 0:
leave_action = input("Do you want leave from table? \nY. \nN. \n")
bet = int(input("Ile chcesz postawic? "))
wallet = wallet - bet
dice = sorted([ri(1, 6) for _ in range(5)])
print(dice)
for i in range(5):
print(f'{i + 1} throw a dice, the drawn value is {dice[i]} ')
result = pair(dice)
result2 = triple(dice)
resultf = full(dice)
resultp = poker(dice)
resultk = kareta(dice)
resultss = smallstreet(dice)
if result:
print('PAIR!')
bounty_from_dice = bounty_from_dice + 1
else:
print('No pair here')
if result2:
print('FAIR!')
bounty_from_dice = bounty_from_dice + 2
else:
print('No fair here')
if resultf:
print("FULL!")
bounty_from_dice = bounty_from_dice + 5
else:
print("No FULL here")
if resultk:
print("KARETA!")
bounty_from_dice = bounty_from_dice + 5
else:
print("No kareta here")
if resultp:
print("POKER!")
bounty_from_dice = bounty_from_dice + 10
else:
print("No poker here")
if resultss:
print("Smallstreet!")
bounty_from_dice = bounty_from_dice + 15
else:
print("No smallstreet here")
hazard_award = bounty_from_dice * int(bet)
wallet = wallet + hazard_award
print("Your win ratio is : " + str(bounty_from_dice))
print("You won from dice poker : " + str(hazard_award))
leave_action = input("Do you want leave from table? \nY. \nN. \n")
print("Your current gold: " + str(wallet))
if leave_action == 'y':
print("You left from dice poker.")
```
Solution 1:[1]
Assuming the code you shared is within a function, you can do multiple things to have a global wallet.
Pass as an argument
wallet = 5
def playDicePoker(wallet):
# [your code here but without the wallet=5 line in the while loop]
return wallet # to recover the new wallet value
# Usage:
wallet = playDicePoker(wallet)
Make it a global variable
This will need more work if you split the code over multiple file.
global wallet
wallet = 5
def playDicePoker():
global wallet
# [your code here but without the wallet=5 line in the while loop]
# Usage:
playDicePoker()
# now wallet is updated
Have a Player class
This will allow you to easily store other informations about your player as well, like the number of wins, the number of game played for each game, and so on
class Player:
def __init__(self, initialWallet):
self.wallet = initialWallet
def playDicePoker(player):
# [your code here but replacing wallet by player.wallet]
# (also no player.wallet = 5 since it's already initialized)
# Usage:
player = Player(5)
playDicePoker(player)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jenny |
