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


Groups > comp.lang.python > #101606

Re: local variable 'juveniles' referenced before assignment

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: local variable 'juveniles' referenced before assignment
Date Wed, 13 Jan 2016 23:32:31 +1100
Lines 25
Message-ID <mailman.99.1452688353.13488.python-list@python.org> (permalink)
References <d668ec4e-509e-48a4-9be5-017077629c6c@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de nccy2MRUR8BfFPkAN+ak1gZdnqxxQwrSC5mEBNWIh4uQ==
Return-Path <rosuav@gmail.com>
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; 'received:209.85.223': 0.03; 'elif': 0.04; "subject:' ": 0.07; 'works.': 0.07; 'cc:addr :python-list': 0.09; 'assigning': 0.09; 'option:': 0.09; 'jan': 0.11; 'def': 0.13; 'wed,': 0.15; 'variables': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'names.': 0.22; 'simpler': 0.22; 'select': 0.23; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; '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; 'instead,': 0.33; 'skip:j 20': 0.33; 'received:google.com': 0.35; 'set.': 0.35; 'skip:p 30': 0.35; 'instead': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'names': 0.38; 'your': 0.60; "you'll": 0.61; 'chrisa': 0.84; 'subject:before': 0.84; 'to:none': 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:cc :content-type; bh=kc5iFHCRrK4azM5aD0oR9g85D4o0QjEC/hPIkhjUqH4=; b=nBHAH97Siqp4sE1JC5FDqQG+XV6FJrLxTA4KJJHmtfF2r1e6SfluIO4xMtQAxR85Dp w5byYDiCD4eiEpq6WonTq5MDbpBfnooAl194F00Hdy5eBctpGTMliY16RAL54lz3e5W5 6z8x4j4jq7DrgCaKOu4+nsGKzkt1+5EZchsfg1GzZ2GUCyxrMOJqvD67tkeiINMgzFaP z8XTW+2/nwf2IPTE5JZk/jCVw4Uuv+lh5xVrfALQolED70mnnURYCZT/qPZjOfa23uGm 1a45Vm2anu36uwAXCPoNdGbIPYCCGGmrC2jEA2ofLYvaEe7w4muhljBAnVfR2DKY+iZi vNiQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=kc5iFHCRrK4azM5aD0oR9g85D4o0QjEC/hPIkhjUqH4=; b=jc6R3lSj3Zeqe+32lEh3krDHeJEYR5Ezzzsk2CVDmSJYqxwhQFdptX/8YoB1js9VU9 efblEfal6dWgLFm5net0AnBlTdw+BL/bP/lQo2CN2W4zOgrdz8bdWRgTY8FPFDca+ecB iGyVcYGVQ2btAqlmZqjBQxoP2EAKVKc2lK5drVUzxzUH/dvXq7DIW93WKYXthRig8vLH SyRwNRKdWCv2FmqqhPI8Od3BkRboVdTlT63RbJwHVCqn84YEvCRHzURyOuQI+kMkZGXk /Phw9OyEVG2VmnD//5Ph97wKKi1NfnvXMV65W295LFWKwY8q1uy0UBEw1VSvmRxxaHXx f+Jw==
X-Gm-Message-State ALoCoQmiuqd2e8UyqEzgGzJ/936QOlPNY2AcmiNTihMNdoUAWrdqqCDkx20VFr/Y9Gy2UMdfjRz5o2vWASXnheJ27vK/Y7FObQ==
X-Received by 10.107.14.73 with SMTP id 70mr94130628ioo.31.1452688351216; Wed, 13 Jan 2016 04:32:31 -0800 (PST)
In-Reply-To <d668ec4e-509e-48a4-9be5-017077629c6c@googlegroups.com>
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:101606

Show key headers only | View raw


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

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