Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: local variable 'juveniles' referenced before assignment Date: Thu, 14 Jan 2016 00:05:15 +1100 Lines: 34 Message-ID: References: <5b66e2de-bc73-4200-a9c1-645d2e3f0467@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ASRNQtdmf0RCYgkwmLqXXwSrTfe7z0Lfd1M+QoOFdLkA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; "subject:' ": 0.07; 'wednesday,': 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; 'quite.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run.': 0.16; 'subject:variable': 0.16; 'utc,': 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; 'chris': 0.26; '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; 'thanks': 0.37; 'received:209.85.213': 0.37; 'january': 0.38; 'received:209': 0.38; 'names': 0.38; 'sure': 0.39; 'your': 0.60; "you'll": 0.61; "'while'": 0.84; '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=snllfPfiUTQn5CFyKruQ1Zpz7t3ouOA5mX2tfPdlp8c=; b=U1YvKwGayIaJeoJnv+Kyr3qrCGiozmzedoXy7ybV8OcUGVNG8IJhiukKagwJFyszD2 viYOZGfBuoS0XPXszcAvu0I3iDr9J8aAc0AF70ZP/8KvKXYYVqW9BPfhMfbwr2EWHY6m evRS4DZCdkn3cVHOPTlQDnlfQR/T/q0tRYoQxM8uCQqHVOLHPBI6xnjE/Rbo+Zhhy60r P7jW17Y91jOV4fXThm88z6e1laLPgdcPD0Ev9taI1oSBuA3JPx3zs1Shw4wy2tY/zZB2 Aff27zpVVYt1FJaLdbrroi/w0Mauf0RBbgxRvaJYrRW7huRDuZ+EhqG43lZTgprnO17V TAQA== 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=snllfPfiUTQn5CFyKruQ1Zpz7t3ouOA5mX2tfPdlp8c=; b=fnO15PiI/U5RyGLb1IyoGikVF2KU/zEosjruEiAhzxOQq46BRwn9hS+4sS/fSuFnzx 2hSU2uI2K9AW8hoQf21twCDwYm+TYuY2vvFbOdADBOD9Jz1b5f6YQUgRkcA2z9UTMrE8 3nkZM7p81AhtDozu19OcGN+0amgiO4bDxRCEQSUAGW1/lu8KtFYS+Pj9gmKc31dMuli3 lMwDwfI6HORWlEKiR3bkbxY7h+QUnsmrCRDhvJuTQ+uOMpPHSxRP6+dGADY8GK9rnZPj g20yy1V4JddnzNAY4Z/ulXDQX70hiYVFSWr92RiGDPYLY5mE2wRbet47p8FmYj44dpP0 dCgg== X-Gm-Message-State: ALoCoQkzTq+GplA0bfX61k2ZCufarQ+hIoOCCRLOQ8j0Ov/nCKhu723/uUwAfsVob8YLu6pxY9ql3iimBW7iYYyKHnim9DfYuA== X-Received: by 10.50.111.169 with SMTP id ij9mr24025186igb.92.1452690315320; Wed, 13 Jan 2016 05:05:15 -0800 (PST) In-Reply-To: <5b66e2de-bc73-4200-a9c1-645d2e3f0467@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101609 On Wed, Jan 13, 2016 at 11:54 PM, 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 >> 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 Not quite. You don't want the menu to run again; you want it to continue to run. Look up the 'while' loop and what it does. ChrisA