這是我的代碼,我想知道你們中的任何人是否可以看看它,看看有什麼不對。迭代時,我一直在字典長度變化中發生的錯誤。我該如何解決?????基本上發生的事情是,我可以添加一些東西,並且他們工作得很好,但是當我真的把蒼蠅拿出來(因爲他們死了),它說它不會工作,因爲字典大小不斷變化。請幫助試圖克服我的字典和循環
import random
from random import randint
deathage = 30
startingpop = 3
rateofreproduction = 0
howmanynewflies = 2
def gennew():
i = 0
list = []
while i < 11:
i = i + 1
x = randint (1,4)
if x == 1:
list.append("A")
elif x == 2:
list.append("T")
elif x == 3:
list.append("C")
else:
list.append("G")
return list
population = {}
def createfly (x):
return ({"dna":x , "age":0})
for i in range(0,startingpop):
population[i] = (createfly(gennew()))
print (population)
def reproduce (x , y):
combos = []
childfly = []
for i in range (0,11):
combos.append((population[x]["dna"][i], population[y]["dna"][i]))
for i in range(0,len(combos)):
x = randint(0,1)
childfly.append(combos[i][x])
return childfly
#each gener**strong text**ation what happens
while len(population) > 0:
for i in population:
population[i]["age"] = population[i]["age"] + 1
if population[i]["age"] > deathage:
population.pop(i)
flies = []
for i in population:
flies.append(i)
for i in range(max(flies), (max(flies) + howmanynewflies)):
whichflyone = int(random.choice(list(population.keys())))
whichflytwo = int(random.choice(list(population.keys())))
population [i] = createfly(reproduce(whichflyone,whichflytwo))
print()
print()
print(population)
非常感謝你 –
如果我已經回答了你的問題,請記住標記爲正確的問題 - 這樣可以避免其他用戶試圖回答已經回答的問題。 – Darkstarone