Python full-stack (a) 6. The basis of the control condition statement Exercise

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/CUFEECR/article/details/102750549

Foreword

Last control statements explained the conditions, but because of its high importance in Python programming, so this blog again for further explain some typical use for conditional control statements, there are more examples of code, allows us to deepen control statements to understand the conditions and procedures to strengthen the focus on optimization.

First, find all the numbers less than 1,000 daffodils

Narcissistic number : refers to a bit number n (n> = 3), on each digital number m is equal to the sum of power itself.
As described for 153: 1 ^ 3 ^ 3 + 3 + 5 ^ 3 = 153.

#1.获取1000以内所有三位数
i = 100
while i < 1000:
    #假设i的百位数是a,十位数是b,个位数是c。
    a = i // 100
    b = (i // 10) % 10
    #求b第二种方法
    #b = (i - a *100) // 10
    c = i % 10
    #2.判断是否为水仙花数
    if a ** 3 + b ** 3 + c ** 3 == i:
        print(i)
    i += 1

Second, obtaining an arbitrary number input by the user, it is determined whether the prime number

Primes : 1 and can only be divisible itself is a prime number, such as 2,3,5,7,11,13 like.

num = int(input('Please input a number which is bigger than 1:'))
# 2.定义变量
i = 2
#创建一个标记用来记录num是否是质数,默认为质数
flag = 1
while i < num:
    #满足下面条件,则num移动不是质数(逆向思维)
    if num % i == 0:
        #一旦符合此判断条件,则证明num不是质数
        flag = 0
    i += 1
if flag:
    print(num,'is a prime')
else:
    print(num,'is not a prime')
    

Third, the loop nest

1. Requirements: Five output in the console, "*", each row five.

method one:

print('*****\n' * 5)

Method Two:

i = 0
while i < 5:
    print('*****')
    i += 1

Method three:

#外层循环控制高度(列),内层循环控制宽度(行)
i = 0
while i < 5:
    j = 0
    while j < 5:
        print('*',end='')
        j += 1
    print()
    i += 1

2.

Requirements: print out the following graphics
*
**
***
****
*****
Method one:

i = 0
while i < 5:
    j = 0
    while j < i + 1:
        print('*',end='')
        j += 1
    print()
    i += 1

Method Two:

i = 0
while i < 5:
    i += 1
    print('*' * i)

3.

Requirements: print out the following graphic




**
*
Method One:

i = 0
while i < 5:
    j = 0
    while j < 5 - i:
        print('*',end='')
        j += 1
    print()
    i += 1

Method Two:

i = 0
while i < 5:
    i += 1
    print('*' * (5 - i))

※ nested loop ※ : arise where another cycle called nested loop in a loop, the outer loop for the former, which is the inner loop, the inner loop nested within the outer loop.
The above-described examples appears nested loops.

Third, in the console output multiplication table

#创建外层循环控制高度
i = 0
while i < 9:
    i += 1
    #创建内层循环控制图形宽度
    j = 0
    while j < i:
        j += 1
        print(f'{j}×{i}={i*j}',end='\t')
    print()
    

Multiplication table generated by running

Four, continue and break

Knowledge:
Continue : it can be used to skip cycles when such

i = 0
while i < 6:
    i += 1
    if i == 2:
        continue
    print(i)
else:
    print('hello')

Export

1
3
4
5
6
hello

BREAK : used to exit the loop immediately, including the else statement, such as

i = 0
while i < 6:
    i += 1
    if i == 2:
        break
    print(i)
else:
    print('hello')

Export

1

Exercise:
guessing game, random numbers from 1 to 10, if you give the correct guess, if you have not guessed it gives an error, giving the user the opportunity nine times, the end result requires the user to have a guess wrong,
that is 1,2, 8, 9, a total of 10 digits, 9 are mistaken. Finally, the correct number.
Ideas: You can get the first user guessed the numbers, I can guess a random number, and the user to guess the numbers had to do comparison, if the number of the cycle had no guess of user numbers, then the numbers that we need to figure .
method one:

import random
#定义一个列表,用来存储用户猜过的数字
lst = []
i = 0
while i < 9:
    number = int(input('Please input a number between 1 and 10:'))
    #把用户猜过的数字添加到列表中
    lst.append(number)
    print('Sorry,you are wrong!!!')
    i += 1

while True:
    #number_x between 1 and 10
    number_x = random.randint(1,10)
    if number_x in lst:
        continue
    else:
        break
print('The right number is',number_x)

Method Two:

import random
#定义一个列表,用来存储用户猜过的数字
lst = []
i = 0
while i < 9:
    number = int(input('Please input a number between 1 and 10:'))
    #把用户猜过的数字添加到列表中
    lst.append(number)
    print('Sorry,you are wrong!!!')
    i += 1
j = 1
while j < 11:
    if j in lst:
        j+=1
        continue
    else:
        break
print('The right number is',j)

Sixth, find all the prime numbers less than 100

i = 2
while i <= 100:
    #创建一个标记
    flag = 1
    #判断i是否为质数:先获取所有有可能成为i的因数的数
    j = 2
    while j < i:
        #判断i是否能被j整除,如果可以,则i不是质数
        if i % j == 0:
            flag = 0
        j += 1
    if flag:
        print(i,end='\t')
    i += 1

Seven, in time to join the program

The program runs for timing:

from time import *
#获取程序开始时间
start = time()
i = 2
while i <= 10000:
    #创建一个标记
    flag = 1
    #判断i是否为质数:先获取所有有可能成为i的因数的数
    j = 2
    while j < i:
        #判断i是否能被j整除,如果可以,则i不是质数
        if i % j == 0:
            flag = 0
        j += 1
    if flag:
        print(i,end='\t')
    i += 1
#获取程序结束时间
end = time()
print()
print('The program runs for',end-start,'second')

Eight, the first optimization prime number

from time import *
#获取程序开始时间
start = time()
i = 2
while i <= 10000:
    #创建一个标记
    flag = 1
    #判断i是否为质数:先获取所有有可能成为i的因数的数
    j = 2
    while j < i:
        #判断i是否能被j整除,如果可以,则i不是质数
        if i % j == 0:
            flag = 0
            #一旦进入判断证明i不是质数,内层循环根本没有必要再继续执行
            break
        j += 1
    if flag:
        print(i,end='\t')
    i += 1
#获取程序结束时间
end = time()
print()
print('The program runs for',end-start,'second')

※ In the line of code flag = 0after joining break, much less ash unnecessary execution, thereby reducing time and enhance operational efficiency.

Nine, the second prime number optimization

from time import *
#获取程序开始时间
start = time()
i = 2
while i <= 10000:
    #创建一个标记
    flag = 1
    #判断i是否为质数:先获取所有有可能成为i的因数的数
    j = 2
    #没有必要对从2到i的书都进行判断
    while j <= i ** 0.5:
        #判断i是否能被j整除,如果可以,则i不是质数
        if i % j == 0:
            flag = 0
            #一旦进入判断证明i不是质数,内层循环根本没有必要再继续执行
            break
        j += 1
    if flag:
        print(i,end='\t')
    i += 1
#获取程序结束时间
end = time()
print()
print('The program runs for',end-start,'second')

※ For a prime, there is no need to judge from 2 to itself, because it is added to sum, for a progressively increasing factor, there must be a number that is gradually reduced from the corresponding numbers per se, they shall be multiplied by the number itself, it is only necessary to determine the square root of this number, but also to a large extent to save time and improve efficiency.
In order to more directly reflect the difference in optimization or not, I will be the upper limit of i are set to 10000 and 100000, and for not optimized, the first optimization, optimization runs and the second statistical time, results as shown below,

Unoptimized

Guess you like

Origin blog.csdn.net/CUFEECR/article/details/102750549