python basis - while the process control loop

while

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.
Syntax Liezi

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

while True:
    print('*1'*100)
    print('*2'*100)

ATM Liezi

# 实现ATM的输入密码重新输入的功能
while True:
    user_db = 'nash'
    pwd_db = '123'

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

ps: Although the above code to achieve the function, but the user password is entered right, he will continue to enter.


while + break

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

while True:
    user_db = 'nash'
    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循环')
# 输出结果password: 123
# username: nash
# password: 123
# login successful
# 退出了while循环


while + continue

continue mean termination of this cycle, the next cycle directly into the
usage Liezi

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

ps: 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 the following location continue where is meaningless. ps: Note that the code to perform the last step, not the last line.


while loop nested

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 = 'nash'
    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')

print('退出了while循环')
# 输出结果password: 123
# username: nash
# password: 123
# login successful
# 退出了while循环


tag control loop exits

# tag控制循环退出
tag = True
while tag:
    user_db = 'nash'
    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: nash
# password: 123
# login successful
# 请输入你需要的命令:q
# q 功能执行
# 退出了while循环


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会在while没有被break时才会执行else中的代码

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374643.html