Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 321 characters in body
Source Link
Super Kai - Kazuya Ito
  • 42.9k
  • 23
  • 259
  • 259

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined yet:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

In addition, even if removing num = 10 as shown below:

# num = 10 # Removed
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

There is the same error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined yet:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined yet:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

In addition, even if removing num = 10 as shown below:

# num = 10 # Removed
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

There is the same error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5
added 8 characters in body
Source Link
Super Kai - Kazuya Ito
  • 42.9k
  • 23
  • 259
  • 259

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined yet:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined yet:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined yet
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5
Source Link
Super Kai - Kazuya Ito
  • 42.9k
  • 23
  • 259
  • 259

In this case of n = num below, n is a local variable and num is a global variable:

num = 10
def test():
 # ↓ Local variable
 n = num
 # ↑ Global variable
 print(n)
 
test()

So, there is no error:

10

But in this case of num = num below, num on the both side are local variables and num on the right side is not defined:

num = 10
def test():
 # ↓ Local variable
 num = num
 # ↑ Local variable not defined
 print(num)
 
test()

So, there is the error below:

UnboundLocalError: local variable 'num' referenced before assignment

So to solve the error above, put global num before num = num as shown below:

num = 10
def test():
 global num # Here
 num = num 
 print(num)
 
test()

Then, the error above is solved as shown below:

10

Or, define the local variable num = 5 before num = num as shown below:

num = 10
def test():
 num = 5 # Here
 num = num
 print(num)
 
test()

Then, the error above is solved as shown below:

5
lang-py

AltStyle によって変換されたページ (->オリジナル) /