Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101605 > unrolled thread
| Started by | Alan Robinson <arobinson@lordlawson.org.uk> |
|---|---|
| First post | 2016-01-13 04:23 -0800 |
| Last post | 2016-01-13 05:26 -0800 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Alan Robinson <arobinson@lordlawson.org.uk> |
|---|---|
| Date | 2016-01-13 04:23 -0800 |
| Subject | local variable 'juveniles' referenced before assignment |
| Message-ID | <d668ec4e-509e-48a4-9be5-017077629c6c@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-13 23:32 +1100 |
| Message-ID | <mailman.99.1452688353.13488.python-list@python.org> |
| In reply to | #101605 |
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
[toc] | [prev] | [next] | [standalone]
| From | Alan Robinson <arobinson@lordlawson.org.uk> |
|---|---|
| Date | 2016-01-13 04:54 -0800 |
| Message-ID | <5b66e2de-bc73-4200-a9c1-645d2e3f0467@googlegroups.com> |
| In reply to | #101606 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-14 00:05 +1100 |
| Message-ID | <mailman.101.1452690317.13488.python-list@python.org> |
| In reply to | #101608 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-13 14:05 +0100 |
| Message-ID | <mailman.102.1452690357.13488.python-list@python.org> |
| In reply to | #101608 |
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
[toc] | [prev] | [next] | [standalone]
| From | Alan Robinson <arobinson@lordlawson.org.uk> |
|---|---|
| Date | 2016-01-13 05:22 -0800 |
| Message-ID | <cfe57c30-6fc0-4d8d-bce3-de10116874fa@googlegroups.com> |
| In reply to | #101610 |
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
[toc] | [prev] | [next] | [standalone]
| From | Alan Robinson <arobinson@lordlawson.org.uk> |
|---|---|
| Date | 2016-01-13 05:26 -0800 |
| Message-ID | <8c528328-0dcd-49df-8d0c-d4519f24ea3a@googlegroups.com> |
| In reply to | #101611 |
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?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web