2019.08.01 learning finishing

2019.08.01 learning finishing

The while loop flow control

1. Grammar

Our wile loops, while loops also known as conditional loop

while 条件:
    code 1
    code 2
    code 3
    ...

while True:
    print('*1'*100)
    print('*2'*100)
while True:
    user_db = 'ming'
    pwd_db = '123'
    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user==user_db and inp_pwd==pwd_db:
        print('login successful')
    else:
        print('username or password error')

The code of the user password is entered, and he will continue to enter.

2. while+break

break off the circulation means terminates the current layer, other code execution.

while True:
    user_db = 'ming'
    pwd_db = '123'
    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user==user_db and inp_pwd==pwd_db:
        print('login successful')
        break
    else:
        print('username or password error')

username : ming
password: 123
login successful

Out of the while loop

3. while+continue

means continue to terminate this cycle, the next cycle directly into

n = 0
while n < 5:
    n += 1
    if n == 3:
        continue
    print(n)

1
2
4
5

continue the code can not be added to the final step in the execution of the loop because the code to add the phrase is meaningless, as shown below

while True:
    if 条件1:
        code1
        code2
        code3
        ...
    else:
        code1
        code2
        code3
        ...

    continue

4. while nested loop

ATM password success also requires a series of command operations, such as withdrawals, such as transfers. And after performing the operation function will exit the command function, i.e. the output to perform an input function q exit while loop function and exits the program ATM.

# 退出内层循环的while循环嵌套
while True:
    user_db = 'nick'
    pwd_db = '123'

    inp_user = input('username: ')
    inp_pwd = input('password: ')

    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')

        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')
# 退出双层循环的while循环嵌套
while True:
    user_db = 'ming'
    pwd_db = '123'

    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and inp_pwd == pwd_db:
        print('login successful')
        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                print(f'{cmd}功能执行')
            break
        break
    else:
        print('username or password error')

print('退出了while循环')

username: ming
password: 123
the Login successful
enter the command you need: q
q functions performed
out of the while loop

5. tag control loop exit

# tag控制循环退出
tag = True
while tag:
    user_db = 'nick'
    pwd_db = '123'

    inp_user = input('username: ')
    inp_pwd = input('password: ')

    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')

        while tag:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                tag = False
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')

print('退出了while循环')

username: Nick
password: 123
the Login successful
enter the command you need: q
q functions performed
out of the while loop

6. while+else

while + else: else will only execute code in else in the while not break

# while+else
n = 1
while n < 3:
    print(n)
    n += 1
else:
    print('else会在while没有被break时才会执行else中的代码')

1
2
else will only execute code in else in the while not break

The for loop flow control

1. Grammar

  1. Dictionary also needs to take multiple values, the dictionary may be unable to use a while loop, this time we can use for loop
name_list = ['nick', 'jason', 'tank', 'sean']
for item in name_list:
    print(item)

nick
jason
tank
sean

  1. The number of cycles for loop is limited by the length of the container type, and the number of while loop requires its own control. The for loop can also be in accordance with the value of the index.
# for循环按照索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
# for i in range(5):  # 5是数的
for i in range(len(name_list)):
    print(i, name_list[i])

0 nick
1 jason
2 tank
3 sean

2. for+break

layer recursive call for the present cycle.

# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    if name == 'jason':
        break
    print(name)

nick

3. for+continue

or recursive call this cycle, the next cycle into the

# for+continue
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    if name == 'jason':
        continue
    print(name)

nick
tank
sean

4. for nested loop

A loop outer loop, the inner loop cycle all.

# for循环嵌套
for i in range(3):
    print(f'-----:{i}')
    for j in range(2):
        print(f'*****:{j}')

-----:0
**:0
**:1
-----:1
**:0
**:1
-----:2
**:0
**:1

5. for+else

When no break else for triggering an internal loop block.

# for+else
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    print(name)
else:
    print('for循环没有被break中断掉')

Nick
Jason
Tank
sean
for the broken loop does not break

6. for realization of loading cycle

import time

print('Loading', end='')
for i in range(6):
    print(".", end='')
    time.sleep(0.2

Loading......

Guess you like

Origin www.cnblogs.com/zhangmingyong/p/11283279.html