流程控制之While循环

流程控制之While循环

1、语法

循环就是一个重复的过程,咱们人须要重复干一个活,那么计算机也须要重复干一个活。ATM验证失败,那么计算机会让咱们再一次输入密码。这个时候就得说出咱们的wile循环,while循环又称为条件循环。python

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

while True:
    print('*1'*100)
    print('*2'*100)
# 实现ATM的输入密码从新输入的功能
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')
    else:
        print('username or password error')

上述代码虽然实现了功能,可是用户密码输对了,他也会继续输入。code

2、while + break

break的意思是终止掉当前层的循环,执行其余代码。input

while True:
    print('1')
    print('2')
    break
    print('3')
1
2

上述代码的break毫无心义,循环的目的是为了让计算机和人同样工做,循环处理事情,而他直接打印1和2以后就退出循环了。而咱们展现下有意义的while+break代码的组合。cmd

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
login successful
退出了while循环

3、while + continue

continue的意思是终止本次循环,直接进入下一次循环class

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

continue不能加在循环体的最后一步执行的代码,由于代码加上去毫无心义,以下所示的continue所在的位置就是毫无心义的。ps:注意是最后一步执行的代码,而不是最后一行。循环

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

    continue

4、while循环的嵌套

ATM密码输入成功还须要进行一系列的命令操做,好比取款,好比转帐。而且在执行功能结束后会退出命令操做的功能,即在功能出执行输入q会退出输出功能的while循环而且退出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')

print('退出了while循环')
# 退出双层循环的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
login successful
请输入你须要的命令:q
退出了while循环

5、tag控制循环退出

# 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
login successful
请输入你须要的命令:q
q 功能执行
退出了while循环

6、while + else

while+else:else会在while没有被break时才会执行else中的代码。程序

# while+else
n = 1
while n < 3:
    print(n)
    n += 1
else:
    print('else会在while没有被break时才会执行else中的代码')
1
2
else会在while没有被break时才会执行else中的代码
相关文章
相关标签/搜索