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
完美 - 你是明星!非常感謝:) –