This is the 13th day of my participation in Gwen Challenge

Example 11

The title

Classic problem: have a pair of rabbits, from be born after the 3rd month from every month gives birth to a pair of rabbits, little rabbit child grows to the 3rd month after every month gives birth to a pair of rabbits again, if rabbit all die, how many is the total number of rabbits that ask every month?

Analysis of the

You can define a function and implement it recursively.

code

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2018-10-10 8:49
# @Author : Manu
# @Site :
# @File : rabbit_num.py
# @Software: PyCharm


def rabbit_num(month) :
    if month == 1 or month == 2:
        return 1
    else:
        return rabbit_num(month-2) + rabbit_num(month-1)

while True:
    month = input('Enter what month')
    if month.isdigit():
        month = int(month)
        print('The number of rabbits in month %d is %d right' %(month, rabbit_num(month)))
    elif month == 'q':
        break
    else:
        print('Wrong input, please re-enter')

Copy the code

The results of

Example 12

The title

Determine how many primes there are between 101 and 200, and print all primes;

Analysis of the

First set a flag bit to default False, enter the loop to determine whether it is a prime number, if it is not a prime number, set it to True, and then output the number with flag bit False as the desired prime number.

code

#! /usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2018-10-7 19:06
# @Author : Manu
# @Site :
# @File : prime_number.py
# @Software: PyCharm

import math

flag = False
count = 0
for i in range(101.201) :for j in range(2.int(math.sqrt(i + 1)) + 1) :if i % j == 0:
            flag = True
            break
    if flag == False:
        count += 1
        print(i, end='\t')
        if count % 5= =0:
            print()
    flag = False
Copy the code

The results of

Examples of 13

The title

Print out all the “daffodil numbers”, the so-called “daffodil number” is a three-digit number, the cube sum of the numbers equals the number itself

Analysis of the

To 100-1000 within the three-digit cycle, find these three-digit numbers in their units, tens, hundreds, and then compare their cube and the three-digit number, if the two are equal, it indicates that the three-digit number is daffodil number;

code

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2018-10-10 8:25
# @Author : Manu
# @Site :
# @File : narcissistic_num.py
# @Software: PyCharm


print('Daffodils List :')
for i in range(100.1000):
    ge = i % 10
    shi = i // 10 % 10
    bai = i // 100

    if i == (ge ** 3 + shi ** 3 + bai ** 3) :print(i)
Copy the code

The results of

Example 14

The title

Decompose a positive integer into prime factors;

Analysis of the

  • If this prime number is exactly equal to n, it indicates that the process of prime factorization is over, and it can be printed;
  • If the n! =k, but n is divisible by k, then print out the value of k and repeat the first step as a new positive integer n divided by the quotient of k;
  • If n cannot be divisible by k, repeat the first step using k+1 as the value of k.

code

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2018-10-8 18:41
# @Author : Manu
# @Site :
# @File : Prime.py
# @Software: PyCharm

def prime(n) :
    print(str(n) + '=')
    if not isinstance(n, int) or n <= 0 :
        print('Please input a valid number ! ')
        exit(0)
    elif n in [1] :
        print(n)
    while n not in [1] :for index in range(2.int(n + 1)) :if n % index == 0:
                n /= index
                if n == 1:
                    print(index)
                else :
                    print(str(index) + "*", end=' ')
                break

num = input('Input the num, enter "q" to quit:)
whilenum ! ='q':
    prime(int(num))
    num = input('Input the num: ')

Copy the code

The results of

Examples of 15

The title

Using the nesting of conditional operators to complete the problem: students with scores >=90 are represented by A, those between 60 and 89 are represented by B, and those below 60 are represented by C.

Analysis of the

Input the score, determine whether it is a number, if it is a number, determine which grade it belongs to, if it is not a number, how to return according to the code setting to re-enter the score or directly exit the program;

code

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2018-10-10 8:33
# @Author : Manu
# @Site :
# @File : score_.py
# @Software: PyCharm

print('Enter result to view registration, enter "Q" to exit')
while True:
    score = input('Enter your grades :')

    if score.isdigit():

        score_rank = int(score) // 10

        if score_rank >= 9:
            print('A')
        elif score_rank >= 6 and score_rank < 9:
            print('B')
        else:
            print('C')
    elif score == 'q':
        break
    else:
        print('Input error, please re-enter! ')

Copy the code

The results of