Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 蟒蛇的动物种群_Python - Fatal编程技术网

Python 蟒蛇的动物种群

Python 蟒蛇的动物种群,python,Python,这是我的代码,它是有效的。但我需要帮助来找出如何根据额外的计算正确地完成编码。 “狼的数量每年以特定的增长率增长 由用户输入。除此之外,每5年狼的数量减少到以前的一半 由于流行性疾病的广泛传播,每年的狼数量。考虑到最初的狼数量, 计算每年的狼数量(提示:使用模运算符进行计算 每年都需要更新狼的数量 根据其增长率,如下所示。此外,除第1年(即仅在第6、11、16年)外,每5年一次,狼的数量 必须是上一年狼数量的一半” 我尝试为这个部件使用嵌套for循环,但无法使其工作 提前非常感谢你 不需要嵌套循

这是我的代码,它是有效的。但我需要帮助来找出如何根据额外的计算正确地完成编码。 “狼的数量每年以特定的增长率增长 由用户输入。除此之外,每5年狼的数量减少到以前的一半 由于流行性疾病的广泛传播,每年的狼数量。考虑到最初的狼数量, 计算每年的狼数量(提示:使用模运算符进行计算 每年都需要更新狼的数量 根据其增长率,如下所示。此外,除第1年(即仅在第6、11、16年)外,每5年一次,狼的数量 必须是上一年狼数量的一半”

我尝试为这个部件使用嵌套for循环,但无法使其工作


提前非常感谢你

不需要嵌套循环-只需在循环中放入
if
语句:

def main():
 wolf = int(input("Enter wolf population (initial): "))
 rabbit = int(input("Enter rabbit population (initial): "))
 grass = float(input("Enter total grass area, initially fertile (in sq yards): "))
 wolf_growth = float(input("Enter wolf growth rate (in percentage): "))
 rabbit_growth = float(input("Enter rabbit annual growth rate (in percentage): "))
 area_growth = float(input("Enter grass area annual growth rate (in percentage): "))

 for year in range(0,21):
     wolf = wolf * (1 + wolf_growth / 100)

     print()
     print("Year  Wolf Population  Rabbit Population Available Grass Area")
     print("%-2d%6d%19d%22.2f" %(year, wolf, rabbit,grass))

main()

您可以看到,行
wolf/=2
仅在
year
可被
5整除时激活(这就是
%
或“模”运算符所做的-在除法后取余数。如果
除以
5
后的余数为
0
,则
可除以
5
),且不等于
0
(因此,不是第一年)。不确定这是否准确地处理了您的需求-您在问题中发布的内容有点让人困惑-但这应该很简单,以适应您的用例。

我感谢您的帮助,谢谢1它确实有帮助,很抱歉让人困惑
for year in range(0,21):
     # wolf population grows annually. You already did this part.
     wolf = wolf * (1 + wolf_growth / 100)

     # every 5 years, except the first year
     if (year % 5 == 0) and (year != 0):
        # wolf population decreases to half of the previous year population
        wolf /= 2

     print()
     print("Year  Wolf Population  Rabbit Population Available Grass     
       Area")
     print("%-2d%6d%19d%22.2f" %(year, wolf, rabbit,grass))