Python100Days study notes --- Day4 loop structure

Loop structure
scenario
if we need to duplicate or execute a certain instruction in the program, such as application control robot to play soccer, but also if the ball does not enter the robot shooting range, then we will have been issued direction to allow the robot to the goal. running instruction. Of course, you may have noticed, there is not only in describing just need to repeat the action, also need to use the previous chapter talking about branching structure. Cite a simple example, we want to achieve a 1 second printed once "hello, world" and continue to print on the screen for an hour every program, we certainly can not directly print ( 'hello, world') phrase 3600 code written all over, if you really do, then the programming work is too boring boring. Therefore, we also need to look at the structure of the cycle, with cyclic structure we can easily control something or some things repeat, repeat, repeat the execution.

Python is configured in a cyclic structure, there are two approaches, one is for-in loop, one is the while loop.

for-in loop
If you know exactly the number of cycles performed on a container or to iterate (will be mentioned later), then we recommend the use of for-in loop, for example, 1 to 100 sums calculation results in the following code ( n = 1 100 n \displaystyle \sum \limits_{n=1}^{100}n )。

"""
用for循环实现1~100求和

Version: 0.1
Author: 骆昊
"""

sum = 0
for x in range(101):
    sum += x
print(sum)

Note that the above code range (101) can be used to construct a range from 0 to 100, can be constructed so that a sequence of integer cycles and used, for example:

range(101)可以产生一个0100的整数序列。
range(1, 100)可以产生一个199的整数序列。
range(1, 100, 2)可以产生一个199的奇数序列,其中2是步长,即数值序列的增量。

Knowing this, we can use the following code to achieve the even sum between 1 and 100.

"""
用for循环实现1~100之间的偶数求和

Version: 0.1
Author: 骆昊
"""

sum = 0
for x in range(2, 101, 2):
    sum += x
print(sum)

May be realized using the same functionality as a branched structure through the loop, the code shown below.

"""
用for循环实现1~100之间的偶数求和

Version: 0.1
Author: 骆昊
"""

sum = 0
for x in range(1, 101):
    if x % 2 == 0:
        sum += x
print(sum)

while loop
if you do not know the structure loop structure specific number of cycles, we recommend using a while loop. capable of generating a while loop or by conversion to an expression control loop bool value, the expression is True cycle continues, the expression evaluates to False cycle ends. Here we (the computer a random number between 1 and 100, enter the number of people own guess, the computer gives prompt information corresponding to guess until people out of the digital computer) through a "Guess" mini-games to see look at how to use the while loop.

"""
猜数字游戏
计算机出一个1~100之间的随机数由人来猜
计算机根据人猜的数字分别给出提示大一点/小一点/猜对了

Version: 0.1
Author: 骆昊
"""

import random

answer = random.randint(1, 100)
counter = 0
while True:
    counter += 1
    number = int(input('请输入: '))
    if number < answer:
        print('大一点')
    elif number > answer:
        print('小一点')
    else:
        print('恭喜你猜对了!')
        break
print('你总共猜了%d次' % counter)
if counter > 7:
    print('你的智商余额明显不足')

The above code uses the break keyword to early termination of the cycle, to note that break that cycle can only terminate it in, which is using a nested loop structure (will be mentioned below) requires attention. In addition to the break, there is another key is continue, it can be used to give up this cycle subsequent code directly recycled into the next round.

Branch structure and the same, but also can be nested loop structure, that may also be configured in the circulation loop structure. The following example demonstrates how to output the multiplication table by a nested loop.

"""
输出乘法口诀表(九九表)

Version: 0.1
Author: 骆昊
"""

for i in range(1, 10):
    for j in range(1, i + 1):
        print('%d*%d=%d' % (i, j, i * j), end='\t')
    print()

Here Insert Picture Description

Here Insert Picture Description

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105205819