1

this question is probably so common that it is being asked every other minute but I've tried to look for answers and couldn't find it. Most likely I wasn't able to phrase the question well enough.

Anyway, I'm working on a small text-game and it has variables, for simplicity let's say water = 100. And I have a function that is the main loop. Let's say the code looks something like this:

water = 100

def main():
    while True:
        water -= 5
        print(water)

main()

Of course, when I run this program it tells me that the variable was referenced before assignment. But if I make the variable inside the function then with each iteration of the loop it will reset the variable to the original 100.

So how do I make this work? Thanks!

Shadylamp
  • 83
  • 10

3 Answers3

3

Use keyword global. In your code declare in function global water before the loop and then your code will work fine.

PatNowak
  • 5,721
  • 1
  • 25
  • 31
  • 4
    This is the correct answer, but it is worth noting that this is generally discouraged in favour of keeping variables within functions and returning values from functions. – Mark Perryman Jan 22 '16 at 15:24
  • 1
    Of course it can be dangerous and can corrupt your program if you don't know how to use it properly. Second option is to define function, which takes one parameter and then as a result of the function it should return new value. – PatNowak Jan 22 '16 at 15:25
  • Thanks, it works! I was a bit hesitant to work with globals because 1) they're frowned upon and 2) I don't really know enough about them so I just dismissed them straight away. I'll go and read about them now. – Shadylamp Jan 22 '16 at 15:27
  • @Shadylamp: now, the question is, do you really need `water` to be global? Can't you define it in `main`, or take it as parameter? – GingerPlusPlus Jan 22 '16 at 17:04
1

If you want to write your code without using globals, you can also write your code to use nonlocal variables as well. However, this may be ever more confusing and inappropriate for what you are trying to do. PatNowak's answer has encouraged you to read about the global keyword. Use the following example as an encouragement to also read about variables that are not exactly local or global.

def create_functions():
    water = 100

    def first_function():
        nonlocal water
        for _ in range(10):
            water -= 5
            print(water)

    def second_function(amount):
        nonlocal water
        water += amount
    return first_function, second_function

main, add_water = create_functions()

main()
add_water(25)
main()
Community
  • 1
  • 1
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
1

if I make the variable inside the function then with each iteration of the loop it will reset the variable to the original 100.

???

def main():
    water = 100
    while water > 0:
        water -= 5
        print(water)

main()

Output:

95
90
85
80
75
70
65
60
55
50
45
40
35
30
25
20
15
10
5
0

See it working online

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52