我是一个非常缺乏经验的程序员,并且想知道你们中是否有人能发现我的代码有什么问题?我一直在扯掉头发。任何帮助,将不胜感激。它也是一个基本错误还是更复杂?
另外,你知道如何修复RunModel函数吗?从理论上讲,它应该工作,但模型只能在其位置打印整个零的负载?
这是我到目前为止所做的:
import random
import sys
Gen0J = 0
Gen0A = 0
Gen0S = 0
SRJ = 0
SRA = 0
SRS = 0
BirthRate = 0
GenNo = 0
NewJ = Gen0J
NewA = Gen0A
NewS = Gen0S
total = NewJ + NewA + NewS
#Print Table Function
def print_table(table):
col_width = [max(len(str(x)) for x in col) for col in zip(*table)]
for line in table:
print("| " + " | ".join("{:{}}".format(x, col_width[i])
for i, x in enumerate(line)) + " |")
def menu():
print("-----Welcome to the AQA Population Model-----\n")
print("1: Set the Generation 0 values")
print("2: Display the Generation 0 values")
print("3: Run the model")
print("4: Export data")
print("5: Quit")
UserInput = int(input("\nPlease select the option 1, 2, 3, 4 or 5:"))
while UserInput != 1 and UserInput != 2 and UserInput != 3 and UserInput != 4 and UserInput != 5:
UserInput = int(input("Invalid input\nPlease select option 1, 2, 3, 4 or 5")) #prompt for user to re-enter their choice
#selection enables the user to access their desired part of the menu
if UserInput == 1:#if the user selected option 1, the gen0_values function will be called
Gen0Set()
elif UserInput == 2: #if the user selected option 2, the display_gen0 function will be called
print("")
table = [['Generation', 'Juveniles', 'Adults', 'Seniles', 'Total'],
[0, Gen0J, Gen0A, Gen0S, (Gen0J + Gen0S + Gen0A)]]
print_table(table)
print("The number of individuals are measured in thousands\n")
menu()
DisplayGen0()
elif UserInput == 3: #if the user selected option 3, the model will be run
print("")
print("Generation\tJuveniles\tAdults\t\tSeniles\t\tTotal\n")
RunModel()
elif UserInput == 4: #if the user selected option 4, they can export the data
print()
ExportData()
else: #if the user selected option 5, the program will end
exit()
def Gen0Set():
global Gen0J, Gen0A, GenoS, SRJ, SRA, SRS, BirthRate, GenNo
print("NOTE: The greenflies will be measured in thousands")
print("NOTE: The survival rate should be a value between 0 and 1 (a decimal number)")
#Gen0J = Generation 0 juvenile values
Gen0J=float(input("What is the initial number of Juveniles? (In thousands)\n"))
#Gen0A = Generation 0 adult values
Gen0A=float(input("What is the initial number of Adults? (In thousands)\n"))
#Gen0S = Generation 0 senile values
Gen0S=float(input("What is the initial number of Seniles? (In thousands)\n"))
#SRJ = Survival rate of juveniles. Float is used as the number could be a decimal.
SRJ = float(input("What is the survival rate of the Juveniles?\n"))
while SRJ < 0 or SRJ > 1:
print ("Invalid input")
SRJ = float(input("What is the survival rate of the Juveniles?\n"))
#SRA = Survival rate of adults. Float is used as the number could be a decimal.
SRA = float(input("What is the survival rate of the Adults?\n"))
#While loop is used to stop user entering invalid inputs
while SRA < 0 or SRA > 1:
print ("Invalid input")
SRA = float(input("What is the survival rate of the Adults?\n"))
#SRS = Survival rate of seniles. Float is used as the number could be a decimal.
SRS = float(input("What is the survival rate of the Seniles?\n"))
#While loop is used to stop user entering invalid inputs
while SRS < 0 or SRS > 1:
print ("Invalid input")
SRS = float(input("What is the survival rate of the Seniles?\n"))
BirthRate = float(input("What is the Birth Rate?\n"))
total = Gen0J + Gen0A + Gen0S
print("\nWhat is the number of New Generations to the model? This number should be between 5 and 25 inclusive.\n")
while True:
GenNo = int(input("Choose the number you would like to input: \n"))
while GenNo < 5 or GenNo > 25:
print("Invalid input")
GenNo= int(input("The number of generations you would like modelled: "))
menu()
return Gen0J, Gen0A, Gen0S, SRJ, SRA, SRS, BirthRate, GenNo
def RunModel():
global Gen0J, Gen0A, Gen0S, SRJ, SRA, SRA, BirthRate, GenNo, NewJ, NewA, NewS, total
for gen in range(0, GenNo + 1):
NewJ = NewA * BirthRate
NewA = NewJ * SRJ
NewS = (NewS * SRS) + (NewA * SRA)
total = NewJ + NewA + NewS
print (gen, "\t\t", NewJ, "\t\t", NewA, "\t\t", NewS, "\t\t", total)
def ExportData():
global Gen0J, Gen0A, Gen0S, SRJ, SRA, SRS, BirthRate, GenNo
print ("Export Data")
filename = input("Filename: ")
while os.path.isfile(filename +".txt"):
overwrite = int(input ("This filename already exists, do you want to overwrite it?\n Enter 1 for yes and 2 for no"))
if overwrite == 1:
filename = filename
print("The file has been overwritten")
print("\nThe data has been saved")
elif overwrite == 2:
export_data()
else:
print("Invalid input")
export_data()
data = ["JUVENILES",str(Gen0J),"ADULTS",str(Gen0A),"SENILES",str(Gen0S),"JUVENILE SURVIVAL RATE",str(SRJ),"ADULT SURVIVAL RATE",str(SRA),"SENILES SURVIVAL RATE",str(SRS),"BIRTH RATE",str(BirthRate),"GENERATIONS",str(No_of_gen)]
with open (filename+".txt", mode = "w", encoding = "utf-8") as my_file:
for item in data:
my_file.write(item + "\n")
print("\nThe data has been saved")
menu()
menu()
答案 0 :(得分:0)
def menu():
print("-----Welcome to the AQA Population Model-----\n")
print("1: Set the Generation 0 values")
print("2: Display the Generation 0 values")
print("3: Run the model")
print("4: Export data")
print("5: Quit")
UserInput = int(input("\nPlease select the option 1, 2, 3, 4 or 5:"))
if UserInput != 1 and UserInput != 2 and UserInput != 3 and UserInput != 4 and UserInput != 5:
UserInput = int(input("Invalid input\nPlease select option 1, 2, 3, 4 or 5")) #prompt for user to re-enter their choice
#selection enables the user to access their desired part of the menu
if UserInput == 1:#if the user selected option 1, the gen0_values function will be called
Gen0Set()
elif UserInput == 2: #if the user selected option 2, the display_gen0 function will be called
print("")
table = [['Generation', 'Juveniles', 'Adults', 'Seniles', 'Total'],
[0, Gen0J, Gen0A, Gen0S, (Gen0J + Gen0S + Gen0A)]]
print_table(table)
print("The number of individuals are measured in thousands\n")
menu()
DisplayGen0()
elif UserInput == 3: #if the user selected option 3, the model will be run
print("")
print("Generation\tJuveniles\tAdults\t\tSeniles\t\tTotal\n")
RunModel()
elif UserInput == 4: #if the user selected option 4, they can export the data
print()
ExportData()
elif UserInput == 5: #if the user selected option 5, the program will end
exit()
else:
menu()
答案 1 :(得分:0)
def ModelRun(): 全球NoOfGenerations 对于范围在(0,NoOfGenerations + 1)中的gen: 全球GenerationJuveniles,GenerationAdults,GenerationSeniles,NewJuveniles,NewAdults,NewSeniles,SurvivalRate_Juvenile,SurvivalRate_Adults,SurvivalRate_Seniles,BirthRate,总计
NewJuveniles = (GenerationAdults) * (BirthRate)
NewAdults = (GenerationJuveniles) * (SurvivalRate_Juvenile)
NewSeniles = (GenerationSeniles) * (SurvivalRate_Seniles) + (GenerationAdults) * (SurvivalRate_Adults)
Total = (NewJuveniles) + (NewAdults) + (NewSeniles)
print str(gen) + "\t", (NewJuveniles), "\t", (NewAdults), "\t", (NewSeniles), "\t", (Total)
菜单()
答案 2 :(得分:0)
import os
def set_gen_0():
global Juveniles
global JuvSurvival
global Adults
global AdultSurvival
global Seniles
global SenileSurvival
global BirthRate
global NumberOfGenerations
print ("Here you can set your given geration 0 values and other information for the model")
Juveniles=int(input("Please enter the amount of juveniles\n"))
JuvSurvival=float(input("Please enter the juvenile survival rate must be between 1 and 0\n"))
while JuvSurvival>1 or JuvSurvival<0:
JuvSurvival=float(input("Invalid number, Please enter the juvenile survival rate must be between 1 and 0\n"))
Adults=int(input("Please enter the amount of adults\n"))
AdultSurvival=float(input("please enter the adult survival rate must be between 1 and 0\n"))
while AdultSurvival>1 or AdultSurvival<0:
AdultSurvival=float(input("Invalid number, Please enter the Adult survival rate must be between 1 and 0\n"))
Seniles=int(input("Please enter the amount of Seniles\n"))
SenileSurvival=float(input("Please enter the senile survival rate, number must be between 1 and 0\n"))
while SenileSurvival>1 or SenileSurvival<0:
SenileSurvival=float(input("invalid number,please enter the Adult survival rate between 1 and 0\n"))
BirthRate=float(input("Enter the Birth rate\n"))
NumberOfGenerations=int(input("Please enter the number of generations you would like the model to run for, must be between 5 and 25\n"))
while NumberOfGenerations>25 or NumberOfGenerations <5:
NumberOfGenerations=int(input("Invalid number, must be between 5 and 25\n"))
menu()
def display_generation0():
print ("There are", Juveniles, "Juveniles")
print ("The Juvenile Survival rate is", JuvSurvival)
print ("There are", Adults, "Adults")
print ("The Adult survival rate is", AdultSurvival)
print ("There are", Seniles, "Seniles")
print ("The Senile survival rate is", SenileSurvival)
print ("The Birthrate is", BirthRate)
print ("The Model will run for", NumberOfGenerations)
menu()
def run_model(Adults,Seniles,Juveniles):
global Wholemodel
Wholemodel=[]
Gen0=[]
TotalPopulation=(Juveniles+Adults+Seniles)
Gen0=[Juveniles,Adults,Seniles,TotalPopulation]
Wholemodel.append(Gen0)
print ("There are", Juveniles, "Juveniles")
print ("There are", Adults, "Adults")
print ("There are", Seniles, "Seniles")
print (Gen0)
for i in range (NumberOfGenerations):
NewJuveniles=(Adults*BirthRate)
NewSeniles=((Seniles*SenileSurvival)+(Adults*AdultSurvival))
NewAdults=(Juveniles*JuvSurvival)
TotalPopulation=(NewJuveniles+NewAdults+NewSeniles)
Gen0=[NewJuveniles, NewAdults, NewSeniles, TotalPopulation]
Wholemodel.append(Gen0)
print ("There are", NewJuveniles, "Juveniles")
print ("There are", NewAdults, "Adults")
print ("There are", NewSeniles, "Seniles")
print (Gen0)
Adults=NewAdults
Seniles=NewSeniles
Juveniles=NewJuveniles
menu()
def export_data(Wholemodel):
global filename
filename=input("Enter a Name for the file: ")
if os.path.isfile(filename):
print("File exists")
decision=input("Do you wish to overwrite it? ")
while decision=="no" or decision=="No":
filename=input("Choose a new file name: ")
f=open(filename,"w")
f.write(str(Wholemodel))
f.close()
break
if decision=="yes" or decision=="Yes":
f=open(filename,"w")
f.write(str(Wholemodel))
f.close()
else:
f=open(filename,"w")
f.write(str(Wholemodel))
f.close()
def menu():
print ("Main Menu")
print ("[1] Set Generation 0 values")
print ("[2] Display the Generation 0 values")
print ("[3] Run Model")
print ("[4] Export data")
print ("[5] Quit")
choice= int(input("What would you like to do\n"))
if choice==1:
set_gen_0()
elif choice==2:
display_generation0()
elif choice==3:
run_model(Adults,Seniles,Juveniles)
elif choice==4:
export_data(Wholemodel)
elif choice==5:
print ("Thanks for using the greenfly population model")
quit()
menu()