0

So I have a file, main.py. The contents are such:

var = 0

def func():
    var = 1

def pr():
    func()
    print(var)

pr()

When run, this prints out 0 instead of my expected 1. Why and how can I get the changes in func() to stick?

fuzzything44
  • 701
  • 4
  • 15
  • You're facing a `scope` problem... Take a look here, it may be useful for you: http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – dot.Py Jul 18 '16 at 14:27

1 Answers1

2

You do it like this by refering to var as a global in func():

var = 0

def func():
    global var
    var = 1

def pr():
    func()
    print(var)

pr()
Ohumeronen
  • 1,769
  • 2
  • 14
  • 28