2017-02-20 8 views
0

私はPythonでアキュムレータを使用しようとしていますが、動作させることができません。私は人口を2から始め、percentIncrease入力で増加させたいが、それは正しく出てこない。私は間違って何をしていますか?私はそれがどのように蓄積しているのか知っていますが、私が試みたすべての試みは失敗しました。Pythonでforループ範囲でアキュムレータを使用するにはどうすればよいですか?

#Get the starting number of organisms 
startingNum = int(input('Enter the starting number of organisms:')) 

#Get the average daily increase 
percentIncrease = float(input('Enter the percentage of average daily increase of organisms:')) 

#Get the number of days to multiply 
number_of_Days = int(input('Enter the number of days to multiply:')) 

population = 0 
cumPopulation = 0 

for number_of_Days in range(1,number_of_Days + 1): 
    population = startingNum 
    cumPopulation += population *(1+percentIncrease) 
    print(number_of_Days,'\t',cumPopulation) 


#So inputs of 2, .3, and 10 should become: 
1 2 
2 2.6 
3 3.38 
4 4.394 
5 5.7122 
6 7.42586 
7 9.653619 
8 12.5497 
9 16.31462 
10 21.209 
+3

あなたは '人口= startingNum'は何が必要ですか?現在の繰り返しごとに 'population'をリセットします。そして、あなたが得ている出力と出力が何であるべきかを入力例として投稿してください。 – Lomtrur

答えて

3

第1日をstartingNumまたはstartingNum * (1+ percentIncrease)として印刷する必要があるかどうかわかりません。

これは何をしたいです:

#Get the starting number of organisms 
startingNum = int(input('Enter the starting number of organisms:')) 

#Get the average daily increase 
percentIncrease = float(input('Enter the percentage of average daily increase of organisms:')) 

#Get the number of days to multiply 
number_of_Days = int(input('Enter the number of days to multiply:')) 

printFormat = "Day {}\t Population:{}" 
cumPopulation = startingNum 

print(printFormat.format(1,cumPopulation)) 

for number_of_Days in range(number_of_Days): 
    cumPopulation *=(1+percentIncrease) # This equals to cumPopulation = cumPopulation * (1 + percentIncrease) 
    print(printFormat.format(number_of_Days+2,cumPopulation)) 

は出力:

Enter the starting number of organisms:100 
Enter the percentage of average daily increase of organisms:0.2 
Enter the number of days to multiply:10 
Day 1 Population:100 
Day 2 Population:120.0 
Day 3 Population:144.0 
Day 4 Population:172.8 
Day 5 Population:207.36 
Day 6 Population:248.832 
Day 7 Population:298.5984 
Day 8 Population:358.31808 
Day 9 Population:429.981696 
Day 10 Population:515.9780352 
Day 11 Population:619.17364224 
+0

'cumPopulation = 0'は無用です。あなたのprintステートメントは、Pythonのバージョンによって異なります。 'print {"} \ t {} "の方が良いでしょう。フォーマット(numberofdays、cumpop))' –

+0

@ Jean-FrançoisFabre、Yup!余分な行は削除されませんでした。更新しました。 –

+0

:最後の値は印刷されません。これは、印刷がループの最初の命令であるためです。 –

-1

population = startingNumをループ外に移動します。

+0

コードにはそれ以上の問題があります。 –

関連する問題