CSC111 Exercises with Global Variables
--D. Thiebaut (talk) 15:57, 24 April 2014 (EDT)
Case 1
A = 3
def f1( x ):
print( "f1", x, A )
print( "A = ", A )
f1( 30 )
|
>>>
A = 3
f1 30 3
>>>
|
Case 2
def f1( x ):
print( "f1", x, A )
A = 3
print( "A = ", A )
f1( 30 )
|
>>>
A = 3
f1 30 3
>>>
|
Case 3
A = 3
def f1( x ):
print( "f1", x, A )
A = 5
print( "A = ", A )
f1( 30 )
print( "A = ", A )
|
>>> A = 3 Traceback (most recent call last): File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 8, in <module> f1( 30 ) File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 4, in f1 print( "f1", x, A ) UnboundLocalError: local variable 'A' referenced before assignment >>> |
Case
A = 3
def f1( x ):
global A
print( "f1", x, A )
A = 5
print( "A = ", A )
f1( 30 )
print( "A = ", A )
|
>>>
A = 3
f1 30 3
A = 5
>>>
|
Case
|
>>>
>>>
|
Case
|
>>>
>>>
|
Case
|
>>>
>>>
|