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 -- https://mail.python.org/mailman/listinfo/python-list
Re: local variable 'juveniles' referenced before assignment
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: local variable 'juveniles' referenced before assignment
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 > >> 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: local variable 'juveniles' referenced before assignment
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 > > >> 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? -- https://mail.python.org/mailman/listinfo/python-list