Analyzing the process while loop

Analyzing the process while loop

First, grammar

Cycle is an iterative process, we need to repeat doing a live person, the computer also need to repeat a live dry. The while loop is also known as a conditional loop.

while 条件:
    <代码块>

When conditions are met, while circulation has been circulated.

二、while+break

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

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')
        break
    else:
        print('username or password error')

print('退出了while循环')

username: Nick
password: 123
the Login successful
out of the while loop

三、while+continue

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

n = 1
while n < 10:
    if n == 8:
        # n += 1  # 如果注释这一行,则会进入死循环
        continue
    print(n)
    n += 1

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. ps: Note that the code to perform the last step, not the last line.

Four, 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} 功能执行')
        break
    else:
        print('username or password error')

print('退出了while循环')

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

Five, tag control loop exits

# 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

六、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

Guess you like

Origin www.cnblogs.com/Lin2396/p/11305768.html