-1

I am trying to use a global variable inside a function, and despite declaring the variable globally, and initializing its value, I get the following error:

Traceback (most recent call last):
 File "test.py", line 11, in <module>
  main()
 File "test.py", line 8, in main
  func_check()
 File "test.py", line 5, in func_check
  value += 45
UnboundLocalError: local variable 'value' referenced before assignment

Following is the code snippet:

value = 0
def func_check():
    value += 45

def main():
    func_check()

if __name__ == "__main__":
    main()
ATP
  • 832
  • 1
  • 15
  • 29

1 Answers1

1

Modify func_check

def func_check():
    global value
    value += 45
f43d65
  • 2,264
  • 11
  • 15