-1

Declared global variable inside func_a() but could not access to it from another function func_b() I am a new in python, sorry if it seems to be odd question

I am using IDLE, when I wrote functions IDLE did not show error but when I call to this function it showed error

Traceback (most recent call last):
  File "<pyshell#110>", line 1, in <module>
    func_b()
  File "<pyshell#109>", line 3, in func_b
    return a + c
NameError: name 'a' is not defined

>>> def func_a():
    global a
    a = 2
    b = 3
    return a + b

>>> def func_b():
    c = 4
    return a + c

>>> func_b()
dastan12
  • 25
  • 5

1 Answers1

3

You never ran function func_a, so the global variable a was never instantiated and assigned. As soon as you run func_a, the variable will be assigned and you can run func_b subsequently.

Julia K
  • 397
  • 3
  • 10