2014-10-27 40 views
1

問題是:「編寫一個程序,預測有機體羣體的近似大小。應用程序應該使用文本框來允許用戶輸入生物體的起始數量,平均每日人口增加量(百分比)以及生物體剩餘繁殖的天數。例如,假設用戶輸入以下值:如何獲得循環顯示初始輸入以及如何接受百分比'%'作爲輸出

開始生物體的數目:2

日均增加:30%

d數AYS乘:10"

這是我到目前爲止的代碼:

s = int(input("Starting number of organisms: ")) 
i = float(input("Average daily increase: ")) 
d = int(input("Number of days to multiply: ")) 
print("Day Approximate\tPopulation") 
for d in range(s, d + 1): 
    add = s * i 
    s = s + add 
    print(d - 1, '\t', s) 

輸出應該是這個樣子:

Day Approximate Population 
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 

這是我的代碼,則輸出「平均每天增加「爲0.30,因爲我不知道如何放入百分比,並將代碼讀取爲〜0.X

Starting number of organisms: 2 
Average daily increase: 0.30 
Number of days to multiply: 10 
Day Approximate Population 
1 2.6 
2 3.38 
3 4.394 
4 5.7122 
5 7.42586 
6 9.653618 
7 12.5497034 
8 16.31461442 
9 21.208998746000002 

謝謝。

回答

0

我不知道,我明白你的問題..也許這樣的:

s = int(input("Starting number of organisms: ")) 
i = float(input("Average daily increase[%]: "))/100.0 
d = int(input("Number of days to multiply: ")) 
first = True 
print("Day Approximate\tPopulation") 
for d in range(s, d + 1): 
    if first: 
     print(1, '\t', s) 
     first = False 
    add = s * i 
    s = s + add 
    print(d - 1, '\t', s) 
+0

對不起,沒真的不知道該怎麼說。我猜如何讓我的輸出從1 2開始,並從那裏而不是1 2.6 – 2014-10-27 02:01:28

+0

@ㅋㅋㅋ:嘗試以上! – gmas80 2014-10-27 02:04:24

+0

謝謝,它工作:) – 2014-10-27 02:06:58

1
i = float(input("Average daily increase [%]: "))/100 
0

str.format()可以幫助你做到這一點,一個例子來自official doc

>>> points = 19.5 
>>> total = 22 
>>> 'Correct answers: {:.2%}'.format(points/total) 
'Correct answers: 88.64%'