The while loop flow control

Process control purposes while

while condition loop

  • Cycle is an iterative process
while 条件:
    代码1
    代码2
    ...
username = 'jin'
password = '1314'
while True:
    inp_username = input('请输入你的用户名>>>:')
    inp_password = input('请输入你的密码>>>:')
    if inp_username == username and inp_password == password:
        print('登录成功')
    else:
        print('登录失败')
# while True 一直循环登录,无论你输入的信息是对还是错.
  • Endless loop
while True:
    print(1+1)
# 无止境的一直循环下去.

while...break

  • break terminates the current cycle
while True:
    print('结束循环')
    break
    代码2
    ...
username = 'jin'
password = '1314'
while True:
    inp_username = input('请输入你的用户名>>>:')
    inp_password = input('请输入你的密码>>>:')
    if inp_username == username and inp_password == password:
        print('登录成功')
        break  # 当用户输入正确的时候呢, break 会结束循环.
    else:
        print('登录失败')

while contine

  • contine termination of this cycle, the next cycle into the
count = 0
while count < 3:
    if count == 1:
        contine
    print(count)
    count += 1

while else

  • while loop when not break, will be performed in the code else.
count = 0
while count < 3:
    print(count)
    count += 1
else:
    print('正常循环完后打印')

while loop nested

username = 'jin'
password = '123'

while True:
    username = 'jin'
    password = '123'
    inp_uaername = input('请输入你的用户名或输入"q"退出>>>:')
    inp_password = input('请输入你的密码或输入"q"退出>>>:')
    if inp_uaername == 'q' or inp_password == 'q':
        break
    if inp_uaername == 'jin' and inp_password == password:
        print('登录成功')
        while True:
            cmd = input('请输入你的指令>>>:')
            if cmd == 'q'
                break
            print('%s功能执行'%(cmd))
        break
    else:
        print('登录失败!')
print('退出循环')

Guess you like

Origin www.cnblogs.com/jincoco/p/11121702.html