Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'function,': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '###': 0.16; 'at,': 0.16; 'exist.': 0.16; 'globals.': 0.16; 'increment': 0.16; 'local.': 0.16; 'scope.': 0.16; 'url:faq': 0.16; 'wrote:': 0.18; 'advance.': 0.19; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'faq': 0.24; 'section.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:- 40': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'ok.': 0.31; 'url:python': 0.33; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'received:google.com': 0.35; 'explains': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'nov': 0.38; 'pm,': 0.38; 'explain': 0.39; 'url:mail': 0.40; 'url:3': 0.61; 'kindly': 0.61; 'provide': 0.64; 'joel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Mnr+hxeXckgtuXqQAyidrjtuLRoS0EvPlMH1CmjwIp0=; b=Xa4ZssVF11/6aVJfYuegSx/D1xZ6h6+1rQRniy4gXu2eaV3N8g8KmNRieiY3EmblQb SYnAI002FjjxFep6n8g1dwIM5/vgP7DnykLY4rr/99uNYVxg4Rng5VSc+QPs6DHLjp2P LyzwJKp2wUTghwWytCgzIegpmAQh4ALgUHufz81ECRPH0xIX9S07zh8qxvVylRIo7mok E+DkgOgL4yavzNzXMcHCUCWl0TdAAmdwDZxxxB6AlQwp+h+SrgfvXl/dNQhcMkefiSEh FEIuDIg6V4q3I5lRenAW2HgdfKZVBAYP/a/KPj45+wuXjlB/Sb9pA7UswkGvk+DRL7Rk bAMg== MIME-Version: 1.0 X-Received: by 10.140.92.2 with SMTP id a2mr41562261qge.51.1415630147316; Mon, 10 Nov 2014 06:35:47 -0800 (PST) In-Reply-To: References: Date: Mon, 10 Nov 2014 09:35:47 -0500 Subject: Re: A syntax question From: Joel Goldstick To: Wolfgang Maier Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415630155 news.xs4all.nl 2831 [2001:888:2000:d::a6]:55702 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80464 On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier wrote: > You may want to read: > > https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value > > from the Python docs Programming FAQ section. > It explains your problem pretty well. > > As others have hinted at, always provide concrete Python error messages and > tracebacks instead of vague descriptions. > > Best, > Wolfgang > > > > On 11/10/2014 12:07 PM, Mok-Kong Shen wrote: >> >> >> I don't understand the following phenomenon. Could someone kindly >> explain it? Thanks in advance. >> >> M. K. Shen >> >> ------------------------------------------------- >> >> count=5 >> >> def test(): >> print(count) >> if count==5: >> count+=0 ### Error message if this line is active, otherwise ok. >> print(count) >> return >> >> test() > > > -- > https://mail.python.org/mailman/listinfo/python-list Your problem is that count is not local. You are reading count from an outer scope. When you try to increment count in your function, it can't because it doesn't exist. Don't use globals. -- Joel Goldstick http://joelgoldstick.com