我正在通过自动处理python来学习python,当我完成最初的章节时,我决定尝试编写一些与作者的目的不同的代码。在毒理学课上,老师说一种蜗牛每年要死12%。所以我决定做个代码来计算。
我做的代码是:
number= input('Number of individuals: ') #tells the number of individuals in the beggining
count = 0 #tells the nuumber of generations
number=int(number)
percent=input('% of loss per generation ') #% of individuals lost in each generation
percent=int(percent)
#while number > 1:
# number= number*(percent*0.01)
# number=round(number,-1)
# count = count +1
#print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')
#if number == 1:
# print(' it took ' + str(count) + ' generations to end with the species')
while True:
if number == 1:
print(' it took ' + str(count) + ' generations to end with the species')
if number >1:
number= number*(percent*0.01)
number=round(number,-1)
count = count +1
print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')
if number == 0 :
break我尝试编写的第一段代码是在注释中编写的,我试图通过另一种方法来解决我的问题。
我的问题是:我想输入个人数量和损失百分比。最后(因为个体不能与自己交配),我想让它告诉我,花了多少代才能以物种结束。当程序比其假定的循环次数多一次(在我使用循环()函数之前,它甚至循环了更多次)问题就开始了,因为我不想要0值,而且当它达到1时,我希望它告诉我它用了多少代才能结束。
我试图寻找答案,但由于我是一个初学者,我可能不知道正确的术语做适当的搜索。在我看来,这个程序应该按照我的意愿运行,但我想我错过了一些我没有得到的东西。
发布于 2017-10-27 09:15:38
让我们在注释中讨论代码,因为它是一个更好的解决方案,只是需要一些改进。代码中有两个主要问题
1)为什么要打印number * 0.1而不是只打印number?它显示出何时应该显示10,我相信这是主要的问题,它误导了你。
print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')2)以下两行必须不在循环中,因为实际上有可能传递1通过und将永远看不到结果消息。这些行必须放在循环之后:
if number == 1:
print(' it took ' + str(count) + ' generations to end with the species')在这里我们得到的结果是:
number= input('Number of individuals: ') # tells the number of individuals in the beggining
count = 0 # tells the nuumber of generations
number=int(number)
percent=input('% of loss per generation ') # % of individuals lost in each generation
percent=int(percent) * 0.01
while number > 1:
number = number * percent
number = round(number)
count = count +1
print('generation ' + str(count) + ':' + str(number) + ' individuals')
print(' it took ' + str(count) + ' generations to end with the species')更新:
如果您不介意的话,还可以应用一些次要的代码样式改进代码:
更新2:
我只修正了循环,但正如阿兰在评论中指出的那样,你计算损失是错误的。百分比应该是1 - int(percent) * 0.01
number = input('Number of individuals: ') # tells the number of individuals in the beggining
number = int(number)
percent = input('% of loss per generation ') # % of individuals lost in each generation
percent = 1 - int(percent) * 0.01
count = 0 # tells the nuumber of generations
while number >= 2:
number = number * percent
count += 1
print('generation {}: {} individuals'.format(count, round(number)))
print(' it took {} generations to end with the species.'.format(count))更新3:
我修改了代码块以改进舍入。是round(number, -1),导致了数十人的四舍五入。
发布于 2017-10-27 09:06:32
看看这个。
number = input('Number of individuals: ') # tells the number of individuals in the beggining
count = 0 # tells the nuumber of generations
number = int(number)
percent = input('% of loss per generation ') # % of individuals lost in each generation
percent = int(percent)
while number > 1:
number = number * (1 - percent * 0.01)
number = int(number)
count = count + 1
print('generation ' + str(count) + ':' + str(number) + ' individuals')
print(' it took ' + str(count) + ' generations to end with the species')包括@Alans的建议。
永无止境的循环问题之所以发生,是因为在接近尾声的时候,只有不到0.5 %的人“死”了。在你的round(number, -1)中,你从5个人中聚集起来,0.3人死亡,4.7人死亡,但四舍五入又使他们5人死亡。int(number)击倒。
发布于 2017-10-27 09:15:47
首先,你计算活蜗牛的数量错误,应该有(100% -损失%)*蜗牛在每一个迭代蜗牛留下活着。第二是条件和圆函数。你的代码很容易被困在损失不足以使前一个数字在四舍五入后降低的点上,例如。4* 0.88 = 3.52,四舍五入后为4。因此,你可以从四舍五入或使用地板代替,这取决于问题的性质。问题是我们如何对待0.52的蜗牛?
在12%的损失和100只个体中,37代以物种为结束而不加舍入,24代后才结束。
下面的代码应该可以工作.
import numpy as np
while True:
if number <= 1: # <= covers values below 1
print(' it took ' + str(count) + ' generations to end with the species')
break;
if number > 1:
number = number*(1-percent*0.01) # number left is 1 - minus loss percent
# number = np.floor(number) # optional floor
count = count + 1
# print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')https://stackoverflow.com/questions/46971011
复制相似问题