-1

I appear to be having this issue in my code and am unable to find a solution so I would be grateful if somebody could please have a look at lines 34,35 and 36 of my code to try and help me remove the required information from the array. (btw I apologise for the comment and general messiness within my code and the fact that it displayed n an external website but I was having issues with formatting on stack overflow)

thanks from Michael Roberts

print(" "*20,"Welcome to the greenfly population model") #printing intro
##########################declaing default variables#########################
generation_input = 10    #default number of generations
birthrate = 2            #default birthrate
juv = 10                 #default number of juveniles
adult = 10               #default number of adults
senile = 10              #default number of seniles
population=[]            # creating an empty list
population.append([juv,adult,senile]) #adding the default population values to the list
jsrate = 1               #default juvenile survival rate
asrate = 1               #default adult survival rate
ssrate = 0               #default senile survival rate

######################setting generation 0 values###################
def g0set():
    global generation_input,birthrate,jsrate,asrate,ssrate #declaring the variables globally
    generation_input = int(input("input number of generations for the model to run for:"))
    birthrate = float(input("input birthrate of model:"))
    juv = int(input("input number of juveniles in the model:"))
    adult = int(input("input number of adults in the model:"))
    senile = int(input("input number of seniles in the model:"))
    population.append([juv,senile,adult])           # adding the new population values into the list
    jsrate = float(input("input the juvenile survival rate of the model:"))
    asrate = float(input("input the adult survival rate of the model:"))
    ssrate = float(input("input the senile survival rate of the model:"))
    main_menu()

#####################displaying generation 0 values#################
def g0display():
    print("the model will run for ",generation_input," generations")
    print("the birthrate of the model is ", birthrate ) 
    print("the number of juveniles in the population model is ",population[0])
    print("the number of adults in the population model is ",population[1])
    print("the number of seniles in the population model is ",population[2])
    print("the juvenile survival rate is ",jsrate)
    print("the adult survival rate is ",asrate)
    print("the senile survival rate is ",ssrate)
    main_menu()



def run_program():
    global juv,adult,senile,generation_input,birthrate,jsrate,asrate,ssrate
    for i in range (0,generation_input): #looping round until it meets the required number of generations
        print (i+1) #printing which generation the loop is on(+1 as i starts from 0)
        print (population) #printing the array on which the population is stored
        juv1 = juv       #storing the previous juvenile values
        adult1 = adult   #storing the previous adult values
        senile1 = senile #storing the previous senile values
        juv= adult * birthrate #applying the birthrate to the adults to get the next generation of juveniles
        adult=juv1 #the previous generation's juveniles become adults
        senile=adult1 #the previous generation's adults become seniles
        juv1 = juv * jsrate #applying the juvenile survival rates with the next generation of juveniles to get the final number of next generation juveniles
        adult1 = adult * asrate #applying the juvenile survival rates with the next generation of adults to get the final number of next generation adults
        senile1 = senile * ssrate #applying the juvenile survival rates with the next generation of seniles to get the final number of next generation seniles
        population.append([juv1,adult1,senile1]) #adding the final values of the next generation to the population array
        juv=juv1 #setting the number of juveniles equal to that of the next generation
        adult=adult1 #setting the number of adults equal to that of the next generation
        senile=senile1 #setting the number of seniles equal to that of the next generation
    main_menu()    #returning to the menu after the program is finished

##################running the main menu#################
def main_menu():
    print("-"*35,"main menu","-"*34)    
    print("input 1 to set generation 0 values")     #printing possible options for the program
    print("input 2 to display generation 0 values") #printing possible options for the program
    print("input 3 to run the population model")    #printing possible options for the program
    choice = int(input()) 
    if choice == 1:
        g0set()
    elif choice == 2:
        g0display()
    elif choice == 3:
        run_program()
    else:
        print("incorrect value entered") 
        main_menu()


main_menu()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
MR0310
  • 13
  • 2
  • Please make the code/url not look like that. – chevybow May 04 '18 at 19:41
  • eeek can you reduce your example text/url? the question looks horrible. – Jean-François Fabre May 04 '18 at 19:41
  • sorry about that and I will try – MR0310 May 04 '18 at 19:44
  • 1
    Take a look at how I formatted it. Simply copy&paste your code, mark it all and hit the **{}** button to indent each line by 4spaces. Or usea texteditor to indent all 4 spaces and copy & paste that , make sure to leave a empty line before the code – Patrick Artner May 04 '18 at 19:45
  • Can you please indicate the specific lines you wanted us to review? Line numbers are not included in formatting. – TinkerTenorSoftwareGuy May 04 '18 at 19:48
  • If you want to replace the values, do not append them, overwrite them singlely or replace the lists content: `population = [[juv,senile,adult]]` - you are adding defaults on top, in `g0set()` you ask new values and `.append()` - afterwards you have another element of 3 things in your population-list - hence it printing duplicates later on – Patrick Artner May 04 '18 at 19:49
  • thank you for the help and sorry about the formatting but it is my first time using this – MR0310 May 04 '18 at 19:58
  • print("the number of juveniles in the population model is ",population[0]) print("the number of adults in the population model is ",population[1]) print("the number of seniles in the population model is ",population[2]) – MR0310 May 04 '18 at 20:00
  • Welcome to StackOverflow. Please read [How to ask](https://stackoverflow.com/help/how-to-ask), and post a [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). You've posted a lot of code that's not relevant to the issue. Please delete all but the crucial bits, and we can help you a lot better! A moment of your time saves that much time for everyone who tries to help. The more work you put into your post, the better answers you'll get. – Jeff Learman May 04 '18 at 22:13

1 Answers1

0

This

##########################declaing default variables#########################
population=[]            # creating an empty list
population.append([juv,adult,senile]) #adding the default population values 

creates a default setting, in

def g0set():
    global generation_input,birthrate,jsrate,asrate,ssrate #declaring the variables globally
    generation_input = int(input("input number of generations for the model to run for:"))
    birthrate = float(input("input birthrate of model:"))
    juv = int(input("input number of juveniles in the model:"))
    adult = int(input("input number of adults in the model:"))
    senile = int(input("input number of seniles in the model:"))
    population.append([juv,senile,adult])           # adding the new population values into the list
    jsrate = float(input("input the juvenile survival rate of the model:"))
    asrate = float(input("input the adult survival rate of the model:"))
    ssrate = float(input("input the senile survival rate of the model:"))
    main_menu()

You add the new settings on top, so now population looks like

[[10,10,10],[juv,senile,adult] # you flipped values of senile&adult here, by chance?

If you want to get a clean slate, do

population = [juv,adult,senile] # flipped to same order as earlier

in g0set() as well.


If you want to only print the last data in your population, use

print(population[-1][0]) # last elem of pop, print juv
print(population[-1][1]) # last elem op pop, print adult
print(population[-1][2]) # last elem of pop, print senile

The other thing that you should change is calling main_menue() on last line of every function: you are creating unnessesary function call stack frames.

Each time you call a function it places data on the stack, creates some space for its own variables etc (simplified) - you are diving ever deeper into the rabbithole. Its better to do it like so:

 while True:    # this will call until you leave your program with ctrl+c or your pc dies
    main_menu()

MainMenue changes to :

def main_menu():
    print("-"*35,"main menu","-"*34)    
    print("input 1 to set generation 0 values")     #printing possible options for the program
    print("input 2 to display generation 0 values") #printing possible options for the program
    print("input 3 to run the population model")    #printing possible options for the program
    choice = int(input()) 
    if choice == 1:
        g0set()
    elif choice == 2:
        g0display()
    elif choice == 3:
        run_program()
    else:
        print("incorrect value entered") 

And every other method also does no longer call main_menu() - it simply ends its job and then implicitly returns None to the main_menu(), it in turn returns None to the main body and the while True: will call your menu again.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • @MR0310 - see edit. You get the lst generations value with `print(population[-1][0])``print(population[-1][1])``print(population[-1][2])` - if you want to remove a specific list element, read here: https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists – Patrick Artner May 04 '18 at 20:12
  • [difference-between-del-remove-and-pop-on-lists](https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists) - in a nutshell: del deletes an element, `elem = l.pop(k)` retrieves the element ant index `k` and deletes it , `del(5)` deletes index 5 from list and `l.remove(5)` removes the first value 5 from the list – Patrick Artner May 04 '18 at 20:15
  • adults and seniles should not be flipped but that is my mistake, and thank you the solution to my array issue as well as for the advice on the main menu. – MR0310 May 04 '18 at 20:16