Difference between revisions of "CSC111 Exercises with Global Variables"
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:57, 24 April 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:57, 24 April 2014 (EDT) | ||
---- | ---- | ||
+ | <br /> | ||
+ | Explain the behavior of the different code sections shown below along with their output. | ||
<br /> | <br /> | ||
=Case 1= | =Case 1= | ||
Line 20: | Line 22: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
− | A = 3 | + | A = 3 |
− | f1 30 3 | + | f1 30 3 |
− | >>> | + | >>> |
</source> | </source> | ||
|} | |} | ||
Line 48: | Line 50: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
− | A = 3 | + | A = 3 |
− | f1 30 3 | + | f1 30 3 |
− | >>> | + | >>> |
</source> | </source> | ||
|} | |} | ||
Line 63: | Line 65: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | A = 3 | |
+ | |||
+ | def f1( x ): | ||
+ | print( "f1", x, A ) | ||
+ | A = 5 | ||
+ | |||
+ | print( "A = ", A ) | ||
+ | f1( 30 ) | ||
+ | print( "A = ", A ) | ||
</source> | </source> | ||
<br /> | <br /> | ||
| | | | ||
− | + | >>> | |
− | >>> | + | <font color="bue">A = 3</font> |
− | <font color="bue">A = 3</font> | + | <font color="red">Traceback (most recent call last): |
− | <font color="red">Traceback (most recent call last): | ||
File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 8, in <module> | File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 8, in <module> | ||
f1( 30 ) | f1( 30 ) | ||
File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 4, in f1 | File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 4, in f1 | ||
print( "f1", x, A ) | print( "f1", x, A ) | ||
− | UnboundLocalError: local variable 'A' referenced before assignment</font> | + | UnboundLocalError: local variable 'A' referenced before assignment</font> |
+ | |||
+ | >>> | ||
− | |||
− | |||
|} | |} | ||
<!-- ---------------------------------------------------------------------------------------------- --> | <!-- ---------------------------------------------------------------------------------------------- --> | ||
<br /> | <br /> | ||
− | =Case | + | |
+ | =Case 4 = | ||
<br /> | <br /> | ||
{| class="wikitable" style="width:100%;" | {| class="wikitable" style="width:100%;" | ||
Line 90: | Line 100: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | A = 3 | |
+ | |||
+ | def f1( x ): | ||
+ | global A | ||
+ | print( "f1", x, A ) | ||
+ | A = 5 | ||
+ | |||
+ | print( "A = ", A ) | ||
+ | f1( 30 ) | ||
+ | print( "A = ", A ) | ||
+ | |||
</source> | </source> | ||
Line 96: | Line 116: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
− | + | A = 3 | |
− | + | f1 30 3 | |
− | >>> | + | A = 5 |
+ | >>> | ||
</source> | </source> | ||
|} | |} | ||
Line 105: | Line 126: | ||
<!-- ---------------------------------------------------------------------------------------------- --> | <!-- ---------------------------------------------------------------------------------------------- --> | ||
<br /> | <br /> | ||
− | =Case = | + | |
+ | =Case 5= | ||
<br /> | <br /> | ||
{| class="wikitable" style="width:100%;" | {| class="wikitable" style="width:100%;" | ||
Line 111: | Line 133: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | def f1( x ): | |
+ | A = [] | ||
+ | print( "f1", x, A ) | ||
+ | A.append( 10 ) | ||
+ | A.append( 20 ) | ||
+ | print( "f1", x, A ) | ||
+ | |||
+ | A = [1, 2, 3] | ||
+ | print( "A = ", A ) | ||
+ | f1( 30 ) | ||
+ | print( "A = ", A ) | ||
+ | |||
</source> | </source> | ||
Line 117: | Line 150: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
− | + | A = [1, 2, 3] | |
− | + | f1 30 [] | |
− | >>> | + | f1 30 [10, 20] |
+ | A = [1, 2, 3] | ||
+ | >>> | ||
</source> | </source> | ||
|} | |} | ||
Line 126: | Line 161: | ||
<!-- ---------------------------------------------------------------------------------------------- --> | <!-- ---------------------------------------------------------------------------------------------- --> | ||
<br /> | <br /> | ||
− | =Case = | + | |
+ | =Case 6= | ||
<br /> | <br /> | ||
{| class="wikitable" style="width:100%;" | {| class="wikitable" style="width:100%;" | ||
Line 132: | Line 168: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | ||
+ | def f1( x ): | ||
+ | global A | ||
+ | A = [] | ||
+ | print( "f1", x, A ) | ||
+ | A.append( 10 ) | ||
+ | A.append( 20 ) | ||
+ | print( "f1", x, A ) | ||
+ | |||
+ | A = [1, 2, 3] | ||
+ | print( "A = ", A ) | ||
+ | f1( 30 ) | ||
+ | print( "A = ", A ) | ||
+ | |||
</source> | </source> | ||
Line 138: | Line 187: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
+ | A = [1, 2, 3] | ||
+ | f1 30 [] | ||
+ | f1 30 [10, 20] | ||
+ | A = [10, 20] | ||
+ | >>> | ||
+ | </source> | ||
+ | |} | ||
+ | |||
+ | <!-- ---------------------------------------------------------------------------------------------- --> | ||
+ | <br /> | ||
+ | =Case 7= | ||
+ | <br /> | ||
+ | {| class="wikitable" style="width:100%;" | ||
+ | | style="width: 50%;" | | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | def f1( x ): | ||
+ | A = x | ||
+ | |||
+ | def f2( ): | ||
+ | print( "f2", A ) | ||
+ | |||
+ | f1( 10 ) | ||
+ | f2() | ||
− | |||
</source> | </source> | ||
+ | <br /> | ||
+ | | | ||
+ | >>> | ||
+ | <font color="red">Traceback (most recent call last): | ||
+ | File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 9, in <module> | ||
+ | f2() | ||
+ | File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 6, in f2 | ||
+ | print( "f2", A ) | ||
+ | NameError: global name 'A' is not defined</font> | ||
+ | >>> | ||
|} | |} | ||
− | + | ||
<br /> | <br /> | ||
− | =Case = | + | =Case 8= |
<br /> | <br /> | ||
{| class="wikitable" style="width:100%;" | {| class="wikitable" style="width:100%;" | ||
Line 153: | Line 236: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | def f1( x ): | |
+ | global A | ||
+ | A = x | ||
+ | |||
+ | def f2( ): | ||
+ | global A | ||
+ | print( "f2", A ) | ||
+ | |||
+ | f1( 10 ) | ||
+ | f2() | ||
</source> | </source> | ||
Line 159: | Line 251: | ||
| | | | ||
<source lang="text"> | <source lang="text"> | ||
− | >>> | + | >>> |
− | + | f2 10 | |
− | + | >>> | |
− | >>> | ||
</source> | </source> | ||
|} | |} |
Latest revision as of 19:34, 24 April 2014
--D. Thiebaut (talk) 15:57, 24 April 2014 (EDT)
Explain the behavior of the different code sections shown below along with their output.
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 4
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 5
def f1( x ):
A = []
print( "f1", x, A )
A.append( 10 )
A.append( 20 )
print( "f1", x, A )
A = [1, 2, 3]
print( "A = ", A )
f1( 30 )
print( "A = ", A )
|
>>>
A = [1, 2, 3]
f1 30 []
f1 30 [10, 20]
A = [1, 2, 3]
>>>
|
Case 6
def f1( x ):
global A
A = []
print( "f1", x, A )
A.append( 10 )
A.append( 20 )
print( "f1", x, A )
A = [1, 2, 3]
print( "A = ", A )
f1( 30 )
print( "A = ", A )
|
>>>
A = [1, 2, 3]
f1 30 []
f1 30 [10, 20]
A = [10, 20]
>>>
|
Case 7
def f1( x ):
A = x
def f2( ):
print( "f2", A )
f1( 10 )
f2()
|
>>> Traceback (most recent call last): File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 9, in <module> f2() File "/Users/thiebaut/Desktop/PYTHON/globalExercises.py", line 6, in f2 print( "f2", A ) NameError: global name 'A' is not defined >>> |
Case 8
def f1( x ):
global A
A = x
def f2( ):
global A
print( "f2", A )
f1( 10 )
f2()
|
>>>
f2 10
>>>
|