Your use of global is not correct when you say global pot += global aibet
Using the global
keyword inside a lower scope (like a function or a class) tells python to use the global version of that variable and not the local version. (You can access a dictionary of the global variables using globals()
).
This means that in every function you modify pot
and aibet
, you need to declare the variable as global
before you use it.
This can get messy and the use of global
variables like this is frowned upon unless absolutely necessary (which for your case it is NOT necessary).
So first, in order to fix your issue, you will need to:
- Add this to your
AIbet
function before you use either variable. Remember, this is telling the interpreter to use the global version of these variables instead of the local version.
global pot
global aibet
- Add this to your
AIspin
function. You use dead
in the if
statement in the global scope later in your code, so dead
also needs to be global
.
global dead
- I'm speculating that your
if dead == deathspin:
statement should be indented and inside the AIspin
function. If this is the case, the only changes you have to make is to add global declarations for pot
, aibet
, and aibudget
like you did for dead
and replace global pot += global aibet
with pot += aibet
. Doing so gives us this for your AIspin
function:
def AIspin():
global dead
global pot
global aibet
global aibudget
print()
print("The AI spins the chamber...")
print("...And pulls the trigger.")
print()
dead = 6
if dead == deathspin:
print("The AI lands on the bad cartridge")
print()
pot += aibet
aibudget -= aibet
print("The pot is currently: $",pot,sep="")
print("The AI has a hand of $",aibudget,".",sep="")
But don't you think this looks bad and is riddled with unnecessary global
statements? Instead, adapt a class
based approach where you don't need to pass around so many global variables. It would look like this:
import random
class RussianRoulette:
def __init__(self):
self.pot = 0
self.playerbudget = 500
self.aibudget = 500
self.playerbet = 0
self.aibet = 10
self.deathspin = 6
self.dead = 6
def ai_bet(self):
print()
self.aibet = random.randint(50,250)
print(f"The AI bets: ${self.aibet:.2f}")
return
def ai_spin(self):
print()
print("The AI spins the chamber...")
print("...And pulls the trigger.")
print()
if self.dead == self.deathspin:
print("The AI lands on the bad cartridge")
print()
self.pot += self.aibet
self.aibudget -= self.aibet
print(f"The pot is currently: ${self.pot:.2f}")
print(f"The AI has a hand of ${self.aibudget:.2f}")
return
You would then use this class like this:
game = RussianRoulette()
game.ai_spin()
game.ai_spin()
game.ai_spin()
Which outputs:
The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $10.00
The AI has a hand of $490.00
The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $20.00
The AI has a hand of $480.00
The AI spins the chamber...
...And pulls the trigger.
The AI lands on the bad cartridge
The pot is currently: $30.00
The AI has a hand of $470.00
Summary: Avoid using global
and use a class approach if possible. You can pack "shared" variables into your class instance. The class instance acts like a container for your variables (in your case). We can access the variables anywhere within the class' methods by prepending the variable name with self
(assuming that's what you named the first argument).
Consider reading:
- Easy to follow tutorial on python classes
- Official documentation on python classes