0

In the first example the code outputs 1. In the second example I get the error code: Local Variable 'x' referenced before assignment. Can anyone explain the reasoning for this?

Example 1:

def num():
    global x
    x = 1

def test():
    print(x)

num()        
test()

Exmaple 2:

def num():
    global x
    x = 1

def test():
    while x < 10:
        print(x)
        x+=1
num()
test()

iminertia
  • 39
  • 6

0 Answers0