繁殖兔Python

2024-06-07 04:25:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我每个月都在写一个让兔子繁殖的程序,我知道已经有问题了,但是我找不到对我有意义的东西,我对python还不熟悉,所以我不能做很多高级的东西。这是我目前为止的代码。一对兔子每个月有4只兔子,所以一个月后有6只兔子,两个月后有18只,三个月后有54只。在

months=input("Enter number of months:")
total=0
while months>0:
  total=months*4+2
  total=total+((total/2)*4)
  print (total)

Tags: of代码程序numberinputtotal意义print
2条回答

这是你想要的吗

months=int(input("Enter number of months:"))
print((3**months)*2)

输出:

^{pr2}$

我把3乘以月数,再乘以2

首先,你的循环是错误的。你没有减少months,所以你有一个无限循环。在

注意问题的状态。我们把初始兔子的数量称为total。每个月,每对(total / 2)产生4个后代。因此,兔子总数为total + (total / 2) * 4,简化为total * 3。又过了一个月。您可以编写一个循环,每月将total乘以3,也可以直接执行total * 3**months。在

>>> def rabbit_population(initial_pop, months):
    return initial_pop * 3**months

>>> rabbit_population(2, 3)
54
>>> 

相关问题 更多 >