问题是“编写一个程序来预测生物种群的大致大小。应用程序应该使用文本框来允许用户输入生物体的起始数量、平均每天的数量增长(百分比),以及生物体将被乘的天数。例如,假设用户输入以下值:
生物起始数:2
平均每日增长率: 30%
乘以天数: 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谢谢。
发布于 2014-10-27 02:00:16
我不确定我是否理解你的问题。也许这是:
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)发布于 2014-10-27 01:56:21
i = float(input("Average daily increase [%]: ")) / 100发布于 2014-10-27 02:05:24
str.format()可以帮助您这样做,这是来自官方医生的一个例子。
>>> points = 19.5
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 88.64%'https://stackoverflow.com/questions/26580086
复制相似问题