0
我试图在Python中运行一个模型,该模型将对列表中的3个项目应用某些计算,然后继续将这些计算应用于一组迭代次数。Python在列表中执行计算并将其添加到列表中
下面的代码是我到目前为止。我是否需要使用诸如modulo或index()来保持if/elif继续增长?或者我会更好地融入功能?
# Nonsense Model for Fish numbers ###
''' Fish numbers, in 100,000 tonnes in coastal waters. Those in the North Sea
increase by 10% every year.
Those in the Irish Sea increase by 15%. Those in the Channel are currently decreasing
the difference by 5%. Also want a total for each year for 10 year prediction.'''
#master list
fish = []
#ask questions
northFish = int(input('How many fish in first year the North Sea?'))
irishFish = int(input('How many fish in first year the Irish Sea?'))
channelFish = int(input('How many fish in first year in The Channel?'))
# add to the list
fish.extend((northFish,irishFish,channelFish))
# display the start
print (fish)
year = int(input('how many years would you like to model?'))
#run the model
for i in range(1,year):
newFish = []
if i == 0:
n = fish[0] * 1.1
newFish.append(n)
elif i == 1:
ir = fish[1] * 1.15
newFish.append(ir)
elif i == 2:
c = fish[2] * 0.95
newFish.append(c)
fish.extend(newFish) # add generated list to the master list
#total = n + i + c
#print('The total for year', i, 'is', total)
print(fish)
这只是给了我3个结果......十年来,我应该有30 ... –
好吧,看我的编辑。你想达到什么目的? – Felix
完美 - 你是明星!非常感谢:) –