The problem of raising rabbits in Python, the basics in basic grammar

Hi everyone, hello! I am a cheese who loves to touch fish ❤

Here comes the whole small case~

It's about raising rabbits,

Let's analyze from ordinary logical thinking

Then turn into programming thinking~

insert image description here

Topic : There is a pair of rabbits,
a pair of rabbits will be born every month from the third month after birth, and a pair
of rabbits will be born every month after the baby rabbit grows to the third month.
If the rabbits are not dead, ask each month What is the total number of rabbits in ?

analyze:

The 0th month is born,
the January rabbit is full moon,
the February rabbit is 2 months old, the March
rabbit is 3 months old, and
the adult rabbit is 3 months old.

The month and number of rabbits that can be pushed forward a few months:

insert image description here

insert image description here

month = int(input('繁殖几个月?: '))
month_0 = 1
month_1 = 0
month_2 = 0
month_elder = 0  
for i in range(month):
    month_0, month_1, month_2, month_elder = month_elder + month_2, month_0, month_1, month_elder + month_2
    print('第%d个月共' % (i + 1), month_0 + month_1 + month_2 + month_elder, '对兔子')
    print('其中0月兔:', month_0)
    print('其中1月兔:', month_1)
    print('其中2月兔:', month_2)
    print('其中成年兔:', month_elder)

I don’t know if you see this,
do you have a little grasp of the syntax of python?

insert image description here

繁殖几个月?: 61个月共 1 对兔子
其中0月兔: 0
其中1月兔: 1
其中2月兔: 0
其中成年兔: 02个月共 1 对兔子
其中0月兔: 0
其中1月兔: 0
其中2月兔: 1
其中成年兔: 03个月共 2 对兔子
其中0月兔: 1
其中1月兔: 0
其中2月兔: 0
其中成年兔: 14个月共 3 对兔子
其中0月兔: 1
其中1月兔: 1
其中2月兔: 0
其中成年兔: 15个月共 4 对兔子
其中0月兔: 1
其中1月兔: 1
其中2月兔: 1
其中成年兔: 16个月共 6 对兔子
其中0月兔: 2
其中1月兔: 1
其中2月兔: 1
其中成年兔: 2

That's it for today's article~

See you in the next article (✿◡‿◡)

insert image description here

Guess you like

Origin blog.csdn.net/m0_74872863/article/details/130170823