Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49071

Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'root': 0.05; '(python': 0.07; 'assignment': 0.07; 'statically': 0.07; 'subject:code': 0.07; 'tkinter': 0.07; '"if': 0.09; 'blue': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'changes': 0.15; '(100,': 0.16; '(it': 0.16; '(x1,': 0.16; 'before.': 0.16; 'determines': 0.16; 'it),': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:variable': 0.16; 'there!': 0.16; 'throw': 0.16; 'tk()': 0.16; 'x2,': 0.16; 'y1,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; 'help.': 0.21; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; "i've": 0.25; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; '(this': 0.29; 'topic': 0.29; "doesn't": 0.30; 'newer': 0.30; "i'm": 0.30; '(which': 0.31; 'lines': 0.31; '"",': 0.31; 'assumes': 0.31; 'occurs': 0.31; 'once,': 0.31; 'file': 0.32; 'quite': 0.32; '(most': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'false': 0.36; 'programming,': 0.36; 'so,': 0.37; 'turn': 0.37; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'remove': 0.60; 'skip:u 10': 0.60; 'simple,': 0.60; 'tell': 0.60; 'color': 0.61; 'new': 0.61; 'first': 0.61; 'back': 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; 'forums': 0.68; 'clicking': 0.73; 'square': 0.74; 'click': 0.77; 'subject:this': 0.83; 'subject:before': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)
Date Mon, 24 Jun 2013 22:05:45 +0200
Organization None
References <b3d3518a-f24a-4c32-a41a-b99145753528@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50848185.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3758.1372104355.3114.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372104355 news.xs4all.nl 15969 [2001:888:2000:d::a6]:57197
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49071

Show key headers only | View raw


pablobarhamalzas@gmail.com wrote:

> Hi there! I'm quite new to programming, even newer in python (this is
> actually the first thing I try on it), and every other topic I've seen on
> forums about my problem doesn't seem to help.
> 
> So, the following lines are intended to draw a white square (which it
> does), turn it to blue when you click on it, and back to white when you
> click on it again (and so on). Here's what I wrote (python 3 syntax):
> 
> 
> from tkinter import *
> 
> root = Tk()
> root.geometry("500x500")
> 
> w = Canvas(root, width=500, height=500)
> w.pack()
> 
> coords = (x1, y1, x2, y2) = (100, 100, 200, 200)
> 
> rect = w.create_rectangle(coords, fill="white")
> isWhite = True
>         
> def change(event):
>     if event.x > x1 and event.x < x2 and event.y > y1 and event.y < y2:
>         if isWhite:
>             w.itemconfig(rect, fill="blue")
>             isWhite = False
>         else:
>             w.itemconfig(rect, fill="white")
>             isWhite = True
>                               
> w.bind("<Button-1>", change)
>       
> root.mainloop()
> 
> 
> The problem occurs when clicking on the white square. The following error
> appears: "if isWhite:
> UnboundLocalError: local variable 'isWhite' referenced before assignment"
> 
> However, the isWhite variable is clearly defined at "True" a few lines
> before. Also, if I remove the lines that change isWhite to False if it's
> True and viceversa, the program doesn't throw any error, but obviously
> doesn't do what I want it to do (it only changes the square color once, as
> isWhite stays set to True).
> 
> What can the problem be? I'm sure it's something really simple, but I
> don't get it... Thank's!

Python statically determines the scope of a variable -- if you rebind a name 
it assumes that the variable is local:

>>> def f():
...     print is_white
...     is_white = 42
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
UnboundLocalError: local variable 'is_white' referenced before assignment

The fix is to tell Python that you want to access the global variable:

>>> def f():
...     global is_white
...     print is_white
...     is_white = 42
... 
>>> is_white = "whatever"
>>> f()
whatever
>>> f()
42

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 12:47 -0700
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 12:53 -0700
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Peter Otten <__peter__@web.de> - 2013-06-24 22:05 +0200
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-24 22:10 +0200
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) John Gordon <gordon@panix.com> - 2013-06-24 20:12 +0000
    Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:33 +0100
    Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Dave Angel <davea@davea.name> - 2013-06-24 16:43 -0400
      Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) John Gordon <gordon@panix.com> - 2013-06-24 21:41 +0000
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 13:19 -0700
    Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:33 +0100
  Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:19 +0100

csiph-web