scope of variable

Rony kara at kara-moon.com
Fri Aug 20 14:33:19 EDT 2010


On Aug 20, 8:25 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Fri, Aug 20, 2010 at 11:09 AM, M B <zna... at telia.com> wrote:
> > Hi,
> > I try to learn python.
> > I don't understand this:
> <snip>
> >>>> dept=0
> <snip>
> >>>> def mud():
> >        dept+=1
> >        print dept
>
> >>>> mud()
> > Traceback (most recent call last):
> >  File "<pyshell#7>", line 1, in <module>
> >    mud()
> >  File "<pyshell#6>", line 2, in mud
> >    dept+=1
> > UnboundLocalError: local variable 'dept' referenced before assignment

Just put a global dept at the start of your function and it will work

Rony



More information about the Python-list mailing list