如何让循环显示初始输入并接受百分比'%'作为输出

1 投票
4 回答
7814 浏览
提问于 2025-04-29 20:08

这个问题是“写一个程序来预测生物种群的大致数量。这个应用程序应该有文本框,让用户输入生物的初始数量、平均每天的增长百分比,以及生物繁殖的天数。例如,假设用户输入以下值:

生物的初始数量: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

谢谢。

暂无标签

4 个回答

0
a = float(input("Please enter starting number of organisms: "))
b = float(input("Please enter average daily increase as percentage: ")) / 100
c = [i for i in range(int(input("Please enter the number of days to multiply: ")))]
Summ = list(map(lambda x : round((a) * (1 + b) ** c.index(x), 3), c))
print("\nDays\t Approximate Population")
for i in range(len(summ)): print(i + 1, "\t", Summ[i])

你的输出结果将会是:

Days     Approximate Population
1        2.0
2        2.6
3        3.38
4        4.394
5        5.712
6        7.426
7        9.654
8        12.55
9        16.315
10       21.209
0

str.format() 可以帮助你实现这个功能,下面是来自官方文档的一个例子:

>>> points = 19.5
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 88.64%'
1
i = float(input("Average daily increase [%]: ")) / 100

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

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)

撰写回答