Difference between revisions of "CSC111 Exercises with Global Variables"

From dftwiki3
Jump to: navigation, search
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 />
 +
=Case 1=
 
<br />
 
<br />
 
{| class="wikitable" style="width:100%;"
 
{| class="wikitable" style="width:100%;"
Line 24: Line 26:
 
</source>
 
</source>
 
|}
 
|}
 +
 +
<br />
 +
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case  2=
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
def f1( x ):
 +
    print( "f1", x, A )
 +
 +
A = 3
 +
print( "A = ", A )
 +
f1( 30 )
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
A =  3
 +
f1 30 3
 +
>>>
 +
</source>
 +
|}
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case 3 =
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
 +
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
<font color="bue">A =  3</font>
 +
<font color="red">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</font>
 +
 +
>>>
 +
</source>
 +
|}
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case  =
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
 +
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
 +
 +
>>>
 +
</source>
 +
|}
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case  =
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
 +
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
 +
 +
>>>
 +
</source>
 +
|}
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case  =
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
 +
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
 +
 +
>>>
 +
</source>
 +
|}
 +
 +
<!-- ---------------------------------------------------------------------------------------------- -->
 +
<br />
 +
=Case  =
 +
<br />
 +
{| class="wikitable" style="width:100%;"
 +
| style="width: 50%;" |
 +
<br />
 +
<source lang="python">
 +
 +
 +
</source>
 +
<br />
 +
|
 +
<source lang="text">
 +
>>>
 +
 +
 +
>>>
 +
</source>
 +
|}
 +
  
 
<br />
 
<br />

Revision as of 19:16, 24 April 2014

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





>>> 
<font color="bue">A =  3</font>
<font color="red">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</font>

>>>


Case





>>> 


>>>


Case





>>> 


>>>


Case





>>> 


>>>


Case





>>> 


>>>