-5

I am currently attempting to run a Python code that has this basic structure:

    var = 0
    def Function2():
        if var == 1:
            ...   
    def Function():
        var = 1
        Function2()

However, when it runs, it doesn't recognize the global variable. Is there any way to fix this?

Emperor Phox
  • 1
  • 1
  • 1
  • 1
    Shot version: You can read global variables from everywhere, but in order to modify them in a function you need to use the `global` keyword on it first. – Grimmy Jul 16 '17 at 09:58

1 Answers1

3

Did you try to write global before the variable name? for example:

a = "Hello"


def change_a():
   global a
   a = "Good"


print(a)

In this mode i've change the content of a from "Hello" to "Good"

Alen Paul Varghese
  • 1,278
  • 14
  • 27
mastrobirraio
  • 135
  • 1
  • 9