[Tutor] urgent help!!!!!!!!!!!

Emile van Sebille emile at fenx.com
Fri Nov 18 23:53:28 CET 2011


On 11/17/2011 3:26 PM ADRIAN KELLY said...
> i know i'm stupid but i have tried everything to get one line of text
> working, i have written out pseudo and read every website.....
> now i am getting this error............
>
> Traceback (most recent call last):
> File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex4 - Copy.py",
> line 24, in <module>
> main()
> File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex4 - Copy.py",
> line 14, in main
> while amount<50:
> UnboundLocalError: local variable 'amount' referenced before assignment
>>

You've seen the fixes, but perhaps more explanation helps too.

Python organizes variables into namespaces.  The normal namespace 
resolution sequence is local, global, builtin.  Assigning to a name 
within a function creates a local variable, otherwise accessing a name 
in a function will use the global or builtin namespaces.  Python decides 
these issues in part during initial loading of the module where it sees 
that you assign to the variable amount within the function, thus 
creating a local variable.  Later, during execution, you first test the 
value of amount (in the while statement), but amount isn't assigned to 
until after the test, ergo, UnboundLocalError.

HTH,

Emile





More information about the Tutor mailing list