python基础---while循环(11)

1、循环语法的基本使用

while 条件:
    代码1
    代码2
    代码3
count = 0
while count < 5:
    print(count)
    count += 1
print('-------->')

2、死循环与效率问题

纯计算无IO的死循环会导致致命的效率问题

3、循环的应用

while True:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        break
    else:
        print('账号或密码错误')
       

4、退出循环的两种方式

①:修改循环条件

将条件改为false,等到下次循环判断条件时才会生效
tag = True
while tag:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        tag = False
    else:
        print('账号或密码错误')

②:使用break

只要运行到break就会立刻终止本层循环
while True:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        break # 立刻终止本层循环
    else:
        print('账号或密码错误')

5、while循环嵌套

①:break方式

while True:
    while True:
        break
    break
while True:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        while True:
            cmd = input("请输入你要操作的业务编号:")
            if cmd =='q':
                break
            print('业务{x}正在运行'.format(x=cmd))

        break # 立刻终止本层循环
    else:
        print('账号或密码错误')

②:改变条件的方式

tag = True
while tag:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        while tag:
            cmd = input("请输入你要操作的业务编号:")
            if cmd =='q':
                tag = False
            print('业务{x}正在运行'.format(x=cmd))

    else:
        print('账号或密码错误')

6、while+continue

continue:结束本次循环,直接进入下一次

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

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

7、while+else

针对break:else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才会运行

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

8、应用案例

①:版本1

count = 0
tag = True
while tag:
    if count == 3:
        print('输错次数超过三次')
        break
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        while tag:
            cmd = input("请输入你要操作的业务编号:")
            if cmd =='q':
                tag = False
            print('业务{x}正在运行'.format(x=cmd))

    else:
        print('账号或密码错误')
        count += 1

②:版本2(优化)

tag = True
count = 0
while count < 3:
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')
    if inp_name == 'zhoushun' and inp_pwd == '123':
        print('登录成功!')
        while True:
            cmd = input("请输入你要操作的业务编号:")
            if cmd =='q':
               break

            print('业务{x}正在运行'.format(x=cmd))
        break

    else:
        print('账号或密码错误')
        count += 1
else:
    print('输错三次,退出')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值