day07-流程控制之while循环

1、循环的语法与基本使用

(ps:下面这种式叫做伪代码)
'''
print(1)
while 条件:
  代码1
  代码2
  代码3
print(3)
'''

count=0
while count < 5: # 5 < 5
    print(count) # 0,1,2,3,4
    count+=1 # 5

print('顶级代码----->')

结果如下:
0 1 2 3 4 顶级代码----->

2、死循环与效率问题

 count=0
 while count < 5: # 5 < 5
 print(count) # 0,1,2,3,4 

while True:
 name=input('your name >>>> ')
 print(name)
 your name >> >> egon
 egon
your name >> >> 

这一种就是交互式的,虽然也是死循环,但是不会过多的浪费资源,因为你要不输入信息的话 ,其实只运行了一行,但是也不推荐这么写代码

纯计算无io的死讯会导致致命的效率问题

# while True:
#     1+1
上面这两句,是说在真的情况下就计算,1+1只是举例而已,以后
可能真的会写这种一直计算的语句出来,而不自知

while 1:
    print('xxxx')

这种就是一直打印,无脑打印
xxxx
xxxx
xxxx
xxxx
xxxx
xxxx
xxxx
Process finished with exit code -1

3、循环的应用

两个问题:
1、重复代码
2、输对了应该不用再重复

username = 'egon'
password = '123'
while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
    else:
        print('账号名或密码错误')

结果如下:
请输入您的账号:阿道夫
请输入您的密码:苦
账号名或密码错误
请输入您的账号:苦
请输入您的密码:花飘万家雪
账号名或密码错误
请输入您的账号:花飘万家雪花飘万家雪
请输入您的密码:工
账号名或密码错误
请输入您的账号:egon
请输入您的密码:123
登录成功
请输入您的账号:

在写代码的时候千万不要出现一模一样的一串代码的重复的情况,这样是非常low的表现,别人一下就看出来是个新手,或者野路子了。


4、退出循环的两种方式
方式一:将条件改为False,等到下次循环判断条件时才会生效

这种方式的话,先设置一个判断条件的初始值,然后等到达到需要的判断条件后,修改条件值即可,跳出循环。

username = 'egon'
password = '123'
tag=True
while tag:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        tag = False # 之后的代码还会运行,下次循环判断条件时才生效
    else:
        print('账号名或密码错误')

    # print('====end====')

结果如下:
请输入您的账号:asf
请输入您的密码:adf
账号名或密码错误
请输入您的账号:aa
请输入您的密码:aa
账号名或密码错误
请输入您的账号:egon
请输入您的密码:123
登录成功

Process finished with exit code 0

方式二:break,只要运行到break就会立刻终止本层循环

这个跟上面的结果是一样的,不过比较上面的较为简便,运行到break就会立刻终止   “本层”   循环(终止本层循环是重点,因为循环可能会有多层,这点要搞清楚)

username = 'egon'
password = '123'

while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        break # 立刻终止本层循环
    else:
        print('账号名或密码错误')

    # print('====end====')

结果如下:
请输入您的账号:asdf
请输入您的密码:asdf
账号名或密码错误
请输入您的账号:aa
请输入您的密码:aa
账号名或密码错误
请输入您的账号:egon
请输入您的密码:123
登录成功

Process finished with exit code 0

5、while循环嵌套与结束

'''
tag=True while tag: while tag: while tag: tag=False # 每一层都必须配一个break while True: while True: while True: break break break
'''

5.1、  break的方式

while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while True:
            cmd=input("输入命令>: ")
            if cmd == 'q':
                break
            print('命令{x}正在运行'.format(x=cmd))
        break # 立刻终止本层循环
    else:
        print('账号名或密码错误')

    # print('====end====')

5.2、改变条件的方式

tag=True
while tag:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while tag:
            cmd=input("输入命令>: ")
            if cmd == 'q':
                tag=False
            else:
                print('命令{x}正在运行'.format(x=cmd))
    else:
        print('账号名或密码错误')

6、while +continue:结束本次循环,直接进入下一次

***** 强调:在continue之后添加同级代码毫无意义,因为永远无法运行

count=0
while count < 6:
    if count == 4:
        count+=1
        continue
        # count+=1 # 错误
    print(count)
    count+=1

7、while +else:针对break

count=0
while count < 6:
    if count == 4:
        count+=1
        continue
    print(count)
    count+=1
else:
    print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才不会运行')

count=0
while count < 6:
    if count == 4:
        break
    print(count)
    count+=1
else:
    print('======>')

应用案列:
版本1:

count=0
tag=True
while tag:
    if count == 3:
        print('输错三次退出')
        break
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while tag:
            cmd=input("输入命令>: ")
            if cmd == 'q':
                tag=False
            else:
                print('命令{x}正在运行'.format(x=cmd))
    else:
        print('账号名或密码错误')
        count+=1

版本2:优化后

count=0
while count < 3:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        while True:
            cmd=input("输入命令>: ")
            if cmd == 'q': # 整个程序结束,退出所有while循环
                break
            else:
                print('命令{x}正在运行'.format(x=cmd))
        break
    else:
        print('账号名或密码错误')
        count+=1
else:
    print('输错3次,退出')
原文地址:https://www.cnblogs.com/xiao-zang/p/12450752.html