Python learning notes: 05 application of branch and loop structure

05 application of branch and loop structure

Classic case

Example 1: Fibonacci sequence

Note: the characteristic of Fibonacci sequence is that the first two numbers of the sequence are 1. Starting from the third number, each number is the sum of the first two numbers. Then the first ten numbers of Fibonacci sequence are: 1 1 2 3 5 8 13 21 34 55

"""
example1 - Enter an integer n,output n Fibonacci number
1 1 2 3 5 8 13
Author: yucui
Date: 2021/7/22
"""

n = int(input('Please enter an integer:'))
a, b = 1, 1
print(a, b, end=' ')
while n - 2:
    c = a + b
    print(c, end=' ')
    b = a
    a = c
    n = n - 1

Enter n = 20

Operation results:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 

Example 2: the problem of buying a hundred chickens for a hundred dollars

A rooster is 5 yuan, a hen is 3 yuan and a chick is 1 yuan. Buy 100 chickens with 100 yuan. Ask how many roosters, hens and chicks are there?

"""
example2 - A hundred dollars for a hundred chickens
 Exhaustive method (brute force cracking method): exhaust all possibilities, and then set conditions to find the solution to the problem
Author: yucui
Date: 2021/7/22
"""
# Rooster values range from 0 to 20
for x in range(0, 21):
    # Hens range from 0 to 33
    for y in range(0, 34):
        z = 100 - x - y
        if z % 3 == 0 and 5 * x + 3 * y + z // 3 == 100:
            print(f'cock{x}only, hen{y}only, chick{z}only')

Example 3: fish distribution

Five people went fishing at night, caught countless fish, and then went to bed when they were tired.
The next day, A woke up first, divided the fish into five, threw away the extra one, and then took his own share,
B the second wakes up, thinks the fish has not been divided, divides the remaining fish into five parts, throws away the extra one and takes his own part,
C. D and E wake up in turn and divide the fish in the same way.
How many fish did they catch at least?

"""
example3 - Exhaustive method for solving the fish division problem
Author: yucui
Date: 2021/7/22
"""
num = 6
while num:
    is_enough = True
    # Check whether the current fish is enough for 5 people
    total = num
    for _ in range(5):
        if (total - 1) % 5 == 0:
            total = (total - 1) // 5 * 4
        else:
            is_enough = False
            break
    if is_enough:
        print(num)
        break
    num += 5

Idea: suppose 6 fish are caught for 5 cycles. If 5 scores can be guaranteed each time, print the number. Otherwise, increase the number of fish by 5 until 5 scores can be met for 5 cycles. At this time, the number of fish is the least.

Example 4: number guessing game

Randomly generate a number of 1-100, enter the number you guess,
The computer gives prompts: 'guess big', 'guess small', 'Congratulations, you guessed right', and displays the number of times you guessed right,
If you guess more than 7 times, it indicates that "the IQ balance is obviously insufficient".

"""
example4 - Figure guessing game
Author: yucui
Date: 2021/7/22
"""

from random import randrange
count = 0
num = randrange(1, 101)
while True:
    count += 1
    guess = int(input('Please enter the number you guessed:'))
    if guess == num:
        print(f'Congratulations, you guessed right. You guessed all together{count}second')
        break
    elif guess < num:
        print('Guess it's small')
    else:
        print('Guess big')
if count > 7:
    print('The IQ balance is obviously insufficient')

Example 5: maximum value, minimum value and average value

Input 10 positive integers in the range of 1-99, and output the maximum value, minimum value and average value

"""
example5 - Enter 10 positive integers 1-99,Find the maximum value, minimum value and average value
Author: yucui
Date: 2021/7/22
"""

max_num = 0
min_num = 100
total = 0
count = 0
while count < 10:
    num = int(input('Please enter:'))
    if num < 1 or num > 99:
        print('invalid input')
        continue
    count += 1
    if max_num < num:
        max_num = num
    if min_num >= num:
        min_num = num
    total += num
average = total / 10
print(f'Maximum{max_num},minimum value{min_num},average value{average:.2f}')

Keywords: Python

Added by Chesso on Tue, 18 Jan 2022 11:35:41 +0200