问题是“编写一个程序,预测生物种群的大致规模。应用程序应使用文本框,允许用户输入生物的起始数量、平均每日种群增长(百分比),以及生物体将要繁殖的天数。例如,假设用户输入以下值:
起始生物数:2
平均每日增长:30%
相乘天数:10“
这是我到目前为止使用for循环的代码,我一直在思考如何执行WHILE循环,请帮助:
organismnum = int(input("Enter the starting number of organism: "))
dailyIncrease = float(input("Enter the average daily increase (in percentage): "))
daysMultiply = int(input("Enter the number of days to multiply: "))
print("Day\t\tApproximate Population\n----\t--------------------------")
for day in range(1, daysMultiply + 1):
if day == 1:
population = organismnum
else:
population *= (1 + dailyIncrease / 100)
print(format(day, "<5d"), format(population, "12,.2f"))
这应该行得通-只需按您的方式格式化结果:
结果可能是一个浮点数,您可以将其“四舍五入”为整数
编辑: 在跟踪所有值的情况下:
相关问题 更多 >
编程相关推荐