0

I'm trying to code a russian roulette game, and I keep getting an error on one specific line of code. I've done some research and can't figure out what's wrong. I get the invalid syntax error on the += part of the line

global pot += global aibet

Not sure why this is happening? Here's the rest of my code for context:

import random

pot = 0
playerbudget = 500
aibudget = 500
playerbet = 0
aibet = 0
deathspin = 6


def AIbet():
    print()
    aibet = random.randint(50,250)
    print("The AI bets: $",aibet,sep="")

def AIspin():
    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()
    global pot += global aibet
    aibudget -= aibet
    print("The pot is currently: $",pot,sep="")
    print("The AI has a hand of $",aibudget,".",sep="")
Nomad556
  • 3
  • 2
  • 1
    I believe that use of `global` is unnecessary. Is there any reason why you’re using a bunch of global variables and printing instead of functions which take/return values? – AMC Dec 12 '19 at 18:53
  • @AlexanderCécile, how would I return the number to the variable outside of the function? – Nomad556 Dec 12 '19 at 19:56
  • For which function? It might help if you explain what the program is meant to do, and I can post that in an answer. – AMC Dec 12 '19 at 20:00
  • I thought I explained that, but here goes: THe program is meant to simulate a gambling game in which an ai makes a bet, and the player can choose to make a bet or fold. If the player folds, a random number is chosen and if it is picked, then the ai loses the amount of money that they bet and it goes into a pot. I would like to return the new values for aibet, pot, and aibudget. – Nomad556 Dec 12 '19 at 20:04
  • _If the player folds, a random number is chosen and if it is picked, then the ai loses_ There are two numbers generated? The aim of the game is just to guess the number 1-6? I'm still not sure I understand, a bunch of things remain unexplained. My question simply was: What are the rules of the game, how does it work? – AMC Dec 12 '19 at 21:18
  • I guess you dont understand Russian Roulette then. Each player can bet any amount, then they essentially roll the dice. If the dice lands on a certain, number, they lose the amount they betted and it is added to to the pot. – Nomad556 Dec 12 '19 at 21:25
  • I thought Russian Roulette involves a gun, you can't lose twice ;) Just to ensure I understand this correctly: A random number is generated. Each player bets a certain amount that their random roll will be equal to that target number. Then, they each roll for a number. If they win, they get the pot, otherwise, they add their bet to the pot. Is that correct? – AMC Dec 12 '19 at 21:30
  • What happens if, say, the second player out of 4 rolls and wins. Do the remaining two automatically lose? This is assuming there is some sort of order to the rolling. – AMC Dec 12 '19 at 21:31

2 Answers2

1

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:

  1. 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
  1. 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
  1. 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:

  1. Easy to follow tutorial on python classes
  2. Official documentation on python classes
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
  • Variable and function names should follow the `lower_case_with_underscores` style. The names aren’t even internally consistent here. – AMC Dec 13 '19 at 19:32
0

That's not how global works. Try this:

global pot 
global aibet
pot += aibet
RafalS
  • 5,834
  • 1
  • 20
  • 25
  • Could you explain what global actually does then? – SyntaxVoid Dec 12 '19 at 18:47
  • `global` makes python look for the name in globals not locals: https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python – RafalS Dec 12 '19 at 18:53
  • @SyntaxVoidsupportsMonica, the purpose of the global is to have the variable remain constant whether it is being used in a function or outside fo a function. – Nomad556 Dec 12 '19 at 19:57