global variable not working inside function. Increment
Helvin Lui
helvinlui at gmail.com
Fri Sep 4 19:57:04 EDT 2009
Wow!!! Thanks a million!! It worked! = DThanks for the fast reply too!
Helvin
On Sat, Sep 5, 2009 at 11:52 AM, Rami Chowdhury <rami.chowdhury at gmail.com>wrote:
> global no_picked
>> no_picked = 0
>>
>> def picked(object, event):
>> no_picked += 1
>> print no_picked
>>
>
> In order to be able to affect variables in the global scope, you need to
> declare them global inside the function, and not at the global scope. So
> your code should read:
>
> no_picked = 0
>
> def picked(object, event):
> global no_picked
> no_picked += 1
> print no_picked
>
> I believe that will work.
>
>
> On Fri, 04 Sep 2009 16:43:27 -0700, Helvin <helvinlui at gmail.com> wrote:
>
> Hi,
>>
>> This increment thing is driving me nearly to the nuts-stage. > <
>>
>> I have a function that allows me to pick points. I want to count the
>> number of times I have picked points.
>>
>> global no_picked
>> no_picked = 0
>>
>> def picked(object, event):
>> no_picked += 1
>> print no_picked
>>
>> Error msg says: UnboundLocalError: local variable 'no_picked'
>> referenced before assignment
>> For some reason, no_picked does not increment, but the printing
>> statement works.
>>
>> Do you know why?
>>
>> (I'm actually writing this for a vtkrenderwindowinteractor.)
>>
>> Helvin
>>
>
>
>
> --
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
>
--
Helvin
"Though the world may promise me more, I'm just made to be filled with the
Lord."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090905/4cb1067f/attachment-0001.html>
More information about the Python-list
mailing list