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?

Recommended Answers

All 3 Replies

If you want to use variables defined in function to be available across all functions then use a class.

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.