0

I am trying to make a Roulette game with a command-prompt interface. Roulette is about betting, and therefor a large part of the game is about the money you lose and win. I am trying to make a variable to tell the player how much money I have before I make another bet. I am not yet finished with the game and therefor only have 3 options available, but I wanted to fix this bug before I continued. I keep getting the error "UnboundLocalError: local variable 'money' referenced before assignment" Here is the code: from random import randint from time import sleep from os import system

def how_much():
    print("How much money do you want to place on this bet?")
    global amount
    amount = int(input())


def game():
    def bet():
        bet = input("What type of bet do you want to make?\n")
        how_much()
        #Start Bet type 1
        if bet == "1":
            roll = randint(1,36)
            if roll <= 3:
                print("You bet correcty!")
                money = money + amount
                game()
            else:
                print("You lose!")
                money = money - amount
                game()
        #Start bet type 2
        elif bet == "2":
            roll = randint(1, 36)
            if roll <= 4:
                print("You bet correcty!")
                money = money + amount
                game()
            else:
                print("You lose!")
                money = money - amount
                game()
        #Start bet type 3
        elif bet == "3":
            roll = randint(1, 36)


        else:
            print("Invalid argument.")
            bet()


    global money
    money = int(1000)
    print("Welcome to Roulette")
    print("Your Wallet has", money, "Euros in it.")
    print("1. First 3 Numbers\n2. First 4 Numbers\n3. One Number\n4. Upper-half or Lower-half\n5. Dozens\n6. Odd or Even\n7. Column\n8. Line\n9. 2 Lines\n10. Red or Black\n11. 2 Neighbors\n12. 4 Neighbors\n0. Exit")
    bet()


game()
  • 1
    `money` is a local variable in your `bet` function. Honestly, using global variables here at all is a bad idea, and you should fundamentally change your approach – juanpa.arrivillaga Nov 24 '18 at 20:04
  • what is the objective of lines such as "global money" ? Python does not need to declare variables as global to create them, but only to reference a global variable from inside a local scope. Which is basically another way of saying, you should almost never really need to use the global variable keyword if you pass your variables responsibly, in most programs. – Paritosh Singh Nov 24 '18 at 20:06
  • How do you suggest I do that? – T Bennnett Nov 24 '18 at 20:07
  • Pass values as arguments to your functions, and have those functions return values that you want to be available outside. Why are you defining `bet` *inside* `game`? Although often defining functions inside other functions can be useful, you aren't make use of any of those reasons here. – juanpa.arrivillaga Nov 24 '18 at 20:08
  • @ParitoshSingh How else would I do it without making the variable global, I want to use it in multiple definitions. – T Bennnett Nov 24 '18 at 20:11
  • @TBennnett do you know how to use parameters and how to return values from functions? You have to do that. Most programs should have maybe one or two global variables, and if they do, those values *should not change*. – juanpa.arrivillaga Nov 24 '18 at 20:12
  • @TBennnett try going through a couple sites online like [these](https://www.learnpython.org/en/Functions) . Look for things like parameter passing/"arguments" and "return". That should help you get started. The idea is this, functions dont always have to just silently run, they can return responses. Infact, most functions are used that way. – Paritosh Singh Nov 24 '18 at 20:15
  • @juanpa.arrivillaga I just took it out, I had left it from a previous trial on fixing the bug. – T Bennnett Nov 24 '18 at 20:18

1 Answers1

0

the problem with your code is that you forgot to put global money inside your bet function. you have declared global money inside your game function, but bet function has no idea of this variable. All you need to do is put global money in the first line of bet function

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • I just did that, it it runs propperly, but everytime it comes up with the menu again, the amount of money in the wallet stays at 1000 – T Bennnett Nov 24 '18 at 20:08
  • That's because you have put `global money , money = int(1000)` i suggest you remove this assignment inside and put it outside of the game function – rawwar Nov 24 '18 at 20:10