How can I find the remainder when dividing 2 integers
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sun Feb 26 17:15:06 EST 2006
On Sun, 26 Feb 2006 21:58:30 +0100, Diez B. Roggisch wrote:
> silverburgh.meryl at gmail.com schrieb:
>> Can you please tell me what is the meaning of
>> UnboundLocalError: local variable 'colorIndex' referenced before
>> assignment
>>
>> in general?
>
> Well, pretty much of what it says: You tried to access a variable without prior assignment to it. Like this:
>
>
> a = b**2 + c**2
>
> Won't work. But if you do
>
> b = 2
> c = 3
> a = b**2 + c**2
>
> it works. I suggest you read a python tutorial - plenty of the out there, google is as always your friend.
Diez' advice is good, but his example is wrong: it will raise a NameError
exception.
When you have a function like this:
def foo(x):
z = x + y
return z
Python's scoping rules treat x and z as local variables, and y as a
global variable. So long as y exists, the function will work.
When you do this:
def bar(x):
y = 2
z = x + y
return z
the scoping rules treat y as a local variable, because you assigned to it.
But with this:
def baz(x)
z = x + y
y = 2
return z
the scoping rules see the assignment to y at compile time, so y is treated
as a local variable. But at run time, y is accessed before it has a value
assigned to it: UnboundLocalError
Hope this helps.
--
Steven.
More information about the Python-list
mailing list