for example, i have this code
def awal():
a = 2
def akhir():
print(a)
awal()
akhir()
well i got error that said a is not defined?
i used global, same error
use return, same error
any idea why?
for example, i have this code
def awal():
a = 2
def akhir():
print(a)
awal()
akhir()
well i got error that said a is not defined?
i used global, same error
use return, same error
any idea why?
If you want to use variables defined in function to be available across all functions then use a class.
Variables created in a function are garbage collected when the function exits so you have to return them to keep a variable. Any tutorial that explains functions covers this https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
def awal():
global a
a=2
return a
def akhir():
a = awal()
print(a)
akhir()
You just miss one thing just check my code..
def awal():
a=2
return a
def akhir():
a = awal()
print(a)
akhir()
This is second code without global variable..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.