我似乎在我的代码中遇到了这个问题而无法找到解决方案,所以如果有人能够查看我的代码中的第34,35和36行来尝试帮助我删除所需的内容,我将不胜感激来自阵列的信息。 (顺便说一句,我为我的代码中的注释和一般混乱而道歉,并且它显示了一个外部网站,但我在堆栈溢出时遇到格式问题)
感谢Michael Robertsprint(" "*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()
答案 0 :(得分:0)
此
##########################declaing default variables#########################
population=[] # creating an empty list
population.append([juv,adult,senile]) #adding the default population 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()
您可以在顶部添加新设置,因此现在人口看起来像
[[10,10,10],[juv,senile,adult] # you flipped values of senile&adult here, by chance?
如果你想要一个干净的名单,那么
population = [juv,adult,senile] # flipped to same order as earlier
在g0set()
中。
如果您只想打印人口中的最后一个数据,请使用
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
您应该更改的另一件事是在每个函数的最后一行调用main_menue()
:您正在创建无效的函数调用堆栈帧。
每次调用函数时,它都会将数据放在堆栈上,为自己的变量创建一些空间等(简化) - 你正在深入研究rabbithole。最好这样做:
while True: # this will call until you leave your program with ctrl+c or your pc dies
main_menu()
MainMenue更改为:
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()
- 它只是结束它的工作然后隐式返回None
到main_menu(),它又将None
返回到main正文和while True:
会再次调用您的菜单。