local variable 'juveniles' referenced before assignment
Chris Angelico
rosuav at gmail.com
Wed Jan 13 07:32:31 EST 2016
On Wed, Jan 13, 2016 at 11:23 PM, Alan Robinson
<arobinson at 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
More information about the Python-list
mailing list