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


Groups > comp.lang.python > #101610

Re: local variable 'juveniles' referenced before assignment

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: local variable 'juveniles' referenced before assignment
Date Wed, 13 Jan 2016 14:05:39 +0100
Organization None
Lines 41
Message-ID <mailman.102.1452690357.13488.python-list@python.org> (permalink)
References <d668ec4e-509e-48a4-9be5-017077629c6c@googlegroups.com> <mailman.99.1452688353.13488.python-list@python.org> <5b66e2de-bc73-4200-a9c1-645d2e3f0467@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de l1ykoTa211mxQSrCGT0zDAtCoBxjixGc/zAUNpSatOrw==
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; 'elif': 0.04; "subject:' ": 0.07; 'wednesday,': 0.07; 'works.': 0.07; 'assigning': 0.09; 'option:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'def': 0.13; 'wed,': 0.15; 'variables': 0.15; '2016': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:variable': 0.16; 'utc,': 0.16; 'wrote:': 0.16; 'names.': 0.22; 'simpler': 0.22; 'select': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'values': 0.28; '13,': 0.29; 'alan': 0.29; 'print': 0.30; 'creating': 0.30; 'code': 0.30; 'call.': 0.30; 'option': 0.31; 'run': 0.33; 'choosing': 0.33; 'instead,': 0.33; 'skip:j 20': 0.33; 'next': 0.35; 'set.': 0.35; 'skip:p 30': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'january': 0.38; 'names': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; "you'll": 0.61; 'choose': 0.68; 'chrisa': 0.84; 'subject:before': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd913e.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101610

Show key headers only | View raw


Alan Robinson wrote:

> On Wednesday, 13 January 2016 12:32:51 UTC, Chris Angelico  wrote:
>> On Wed, Jan 13, 2016 at 11:23 PM, Alan Robinson
>> <arobinson@lordlawson.org.uk> wrote:
>> > def menu():
>> >     option = int(input("Please select an option: \n 1: Set Generation 0
>> >     Values \n 2: View Generation 0 Values \n 3: Run Model \n 4: Print
>> >     values"))
>> >
>> >     if option == 1:
>> >         juveniles,adults,seniles = setGen()
>> >     elif option == 2:
>> >         displayGen()
>> >     elif option == 3:
>> >         runModel(juveniles,adults,seniles)
>> >     elif option == 4:
>> >         print(juveniles,adults,seniles)
>> >     menu()
>> >
>> 
>> This is a classic use of recursion instead of iteration. When you call
>> menu() again, you're creating a completely new 'slot' for the new
>> function; it has its own set of names. Assigning to names in one call
>> of menu() has no effect on any other call.
>> 
>> Instead, look into the way a while loop works. You'll find that your
>> code is simpler and clearer, plus your variables will stay set.
>> 
>> ChrisA
> thanks I need the menu to run again not sure how to do that though

def menu():
    option = 1 # make sure setGen is invoked on first iteration
    while option: # choosing 0 ends the while loop
        if option == 1:
           juveniles, adults, seniles = setGen()
        elif option == 2:
           ...
        option = int(input(...)) # choose option for the next iteration

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


Thread

local variable 'juveniles' referenced before assignment Alan Robinson <arobinson@lordlawson.org.uk> - 2016-01-13 04:23 -0800
  Re: local variable 'juveniles' referenced before assignment Chris Angelico <rosuav@gmail.com> - 2016-01-13 23:32 +1100
    Re: local variable 'juveniles' referenced before assignment Alan Robinson <arobinson@lordlawson.org.uk> - 2016-01-13 04:54 -0800
      Re: local variable 'juveniles' referenced before assignment Chris Angelico <rosuav@gmail.com> - 2016-01-14 00:05 +1100
      Re: local variable 'juveniles' referenced before assignment Peter Otten <__peter__@web.de> - 2016-01-13 14:05 +0100
        Re: local variable 'juveniles' referenced before assignment Alan Robinson <arobinson@lordlawson.org.uk> - 2016-01-13 05:22 -0800
          Re: local variable 'juveniles' referenced before assignment Alan Robinson <arobinson@lordlawson.org.uk> - 2016-01-13 05:26 -0800

csiph-web