f



local variable 'juveniles' referenced before assignment

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()


def setGen():
    #enter number of juveniles
    juveniles = int(input("How many juveniles are in the total popluation?"))
    #enter number of adults
    adults = int(input("How many Adults are in the total popluation?"))
    #enter number of seniles
    seniles = int(input("How many Seniles are in the total popluation?"))
    #enter juvenilesenile survival rates
    return(juveniles,adults,seniles)
menu() 
when I go to print I get the above error I can't see a solution please help
0
Alan
1/13/2016 12:23:05 PM
comp.lang.python 77058 articles. 3 followers. Post Follow

6 Replies
163 Views

Similar Articles

[PageSpeed] 57

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
0
Chris
1/13/2016 12:32:31 PM
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

0
Alan
1/13/2016 12:54:36 PM
On Wed, Jan 13, 2016 at 11:54 PM, Alan Robinson
<arobinson@lordlawson.org.uk> 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

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
0
Chris
1/13/2016 1:05:15 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
>> <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

0
Peter
1/13/2016 1:05:39 PM
On Wednesday, 13 January 2016 13:06:11 UTC, Peter Otten  wrote:
> 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

that's really helpful I understand whats happening now I just need to work out how to do this loop as I am new it looks difficult to do here goes

0
Alan
1/13/2016 1:22:28 PM
On Wednesday, 13 January 2016 13:23:04 UTC, Alan Robinson  wrote:
> On Wednesday, 13 January 2016 13:06:11 UTC, Peter Otten  wrote:
> > 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
> 
> that's really helpful I understand what's happening now I just need to work out how to do this loop as I am new it looks difficult to do here goes not too sure what you mean by make sure setGen is invoked on the first iteration?

0
Alan
1/13/2016 1:26:56 PM
Reply:

Similar Artilces:

UnboundLocalError: local variable '_[1]' referenced before assignment
Hello everyone, I get this error on python 2.6.1 on mac os x 10.6 : UnboundLocalError: local variable '_[1]' referenced before assignment here's the code that raises this: params = [ self.__formatData(paramProcFunc, query, p) for p in params ] what I don't get is that it worked on mac os x 10.5 (python 2.5.x) but it no longer works. I tried the following and it works : r = [] for p in params: r.append(self.__formatData(paramProcFunc, query, p)) params = r Does anyone understand what is going on here? Thank you, Gabriel On Dec 9, 10:17=A0am, G...

HELP:UnboundLocalError: local variable '_nntp' referenced before assignment
Hi all, I am in the process of writing an app that will handle news feeds and therefore using the 'nntplib' When I issue the connect command all goes will but when I issue the 'quit()' command I get the following error message: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__ return self.func(*args) File "./pnews.py", line 26, in callconnect _nntp.quit() UnboundLocalError: local variable '_nntp' referenced before assignment The code below illustrates...

Error: UnboundLocalError: local variable 'PfFlag' referenced before assignment
$$ TestCase ID : 001 Step : deleteDvc,206268 Result Eval type : XmlChk Step : deleteDvc,206269 Result Eval type : XmlChk Traceback (most recent call last): File "C:\UDR2\UDRxmlGateway.py", line 388, in <module> ParseAll() File "C:\UDR2\UDRxmlGateway.py", line 371, in ParseAll if (PfFlag =3D=3D 1): UnboundLocalError: local variable 'PfFlag' referenced before assignment =20 =20 PfFlag gets assigned in a for loop in line 365 for i in range(PfFlagArrSize): if (PfFlagArr[i] =3D=3D 1): ---> PfFlag =3D int(1) ...

ndefined function or variable 'y'. The first assignment to a local variable determines its class. Function 'Embedded MATLAB Function' (#52.384.385), line 19, column 2: "y"
Hi everybody I have a serious problem and i need your help. i use embedded matlab function in simulink to generate some matrices which i use as input to other bloc on simulink. But i got this error while the simulation is running: Undefined function or variable 'y'. The first assignment to a local variable determines its class. Function 'Embedded MATLAB Function' (#52.384.385), line 19, column 2: "y" My code is function y = fcn(u) Ap=[0.9580 0 0 0;0 0.9418 0 0;0 0 0.9048 0;0 0 0 0.9277]; Bp=[0.25 0;0.25 0;0 0.5;0 0.5]; Cp=[0.1678 0 0.9516 0;0 0.2329...

'^=' and '~='?
Hello, What is the difference between '^=' and '~='? Thanks, Duckhye ...

'is not' or '!='
A newbie question to you; what is the difference between statements like: if x is not None: and if x != None: Without any context, which one should be preferred? IMHO, the latter is more readable. On 2014-08-18 21:35, ElChino wrote: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: > > Without any context, which one should be preferred? > IMHO, the latter is more readable. > "x == y" tells you whether x and y refer to objects that are equal. "x is y&qu...

which is more 'pythonic' / 'better' ?
hi, there are 2 versions of a simple code. which is preferred? === if len(line) >= (n+1): text = line[n] else: text = 'nothing' === === try: text = line[n] except IndexError: text = 'nothing' === which is the one you would use? thanks, gabor gabor wrote: > hi, > > there are 2 versions of a simple code. > which is preferred? > > > === > if len(line) >= (n+1): > text = line[n] > else: > text = 'nothing' > === > > > === > try: > text = line[n] > except IndexError: > text = '...

Why is 'None' not assignable but 'True'/'False' are?
In Python 2.4.1: >>> None = 99 SyntaxError: assignment to None >>> True = 99 >>> False = 99 >>> True == False True ----------------------- So why is 'None' special? Rodney Maxwell <rodney.maxwell@gmail.com> wrote: > In Python 2.4.1: > > >>> None = 99 > SyntaxError: assignment to None > >>> True = 99 > >>> False = 99 > >>> True == False > True > ----------------------- > So why is 'None' special? A legacy/backwards compatibility issue: None has been there 'foreve...

How to set variables 'lang' and 'pma_lang' to iso-8859-1 ?
Hello to you all I'm a newbie in PHP. I've a trouble with the codage. When I launch phpinfo() (PHP 4.3.9) through phpmyadmin (2.6.0.rc1 on a RedHat Avanced Server 4 update 1), I get in the 'PHP Variables' section : ........... _REQUEST["lang"] = fr-utf-8 ........... _REQUEST["pma_lang"] = fr-utf-8 .......... _GET["pma_lang"] = fr-utf-8 .......... _COOKIE["pma_lang"] = fr-utf-8 I want to replace fr-utf-8 by fr-iso-8859-1. But I don't know where ? I've check the config.inc.php in phpmyadmin directory. I've set the $cfg['...

UnboundLocalError: local variable 'colorIndex' referenced
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and init to 0, like this colorIndex = 0 and in one of my functions, I increment it by 1 def myFunc colorIndex += 1 silverburgh.meryl@gmail.com wrote in news:1140987642.195734.187540 @t39g2000cwt.googlegroups.com: > Can you please tell me what is the meaning this error in general? > > UnboundLocalError: local variable 'colorIndex' referenced before > assignment > ...

Can't figure out where SyntaxError: can not delete variable 'x' referenced in nested scope us coming from in python >=2.6
Say I have module foo.py: def a(x): def b(): x del x If I run foo.py under Python 2.4.4 I get: File "foo.py", line 4 del x SyntaxError: can not delete variable 'x' referenced in nested scope Under Python 2.6 and Python 3.0 I get: SyntaxError: can not delete variable 'x' referenced in nested scope The difference is under Python 2.4 I get a traceback with the lineno and offending line, but I do not get a traceback in Pythons 2.6 and 3.0. The reason why I as...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal''''''''''''''
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria" FROM "criteria" WHERE "criteria" UPDATE/APPEND "field selections" RecordSource "qryExample" = above text strings" _______________________________________________________________________ When i am running a SELECT query in this manner it works fine with no problems, and accepts the values of specified linked for...

Python 'for' as Scheme 'let'
We show that the 'for' operator of Python quite unexpectedly can be represented by a 'let' form of Scheme. What looks in Python like for k in range(1,4): print k looks in Scheme as (breset r (let ((k (range r 1 4))) (display k) (newline) )) This iteration construct uncannily resembles Python. It nests and supports the premature termination and skipping of iterations at any level. We should also emphasize that no intermediate lists are constructed! The 'let' in the above code is the ordinary Scheme let. The macro range has been described in the arti...

Are 'mode' and 'coding' variables?
Hi there, One can write "-*-mode:emacs-lisp;coding:utf-8;-*-" on the head of a file to force emacs to use emacs-lisp-mode and utf-8 coding. I am wondering what are `mode' and `coding'. I use 'C-h v' on them and there are no such variables. Thank you for your help. -- Leo On Thu, Nov 23 2006, Leo wrote: > One can write "-*-mode:emacs-lisp;coding:utf-8;-*-" on the head of a > file to force emacs to use emacs-lisp-mode and utf-8 coding. I am > wondering what are `mode' and `coding'. I use 'C-h v' on them and > there are no such...

Web resources about - local variable 'juveniles' referenced before assignment - comp.lang.python

Resources last updated: 1/24/2016 6:38:01 PM