博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
控制流程之while循环
阅读量:4692 次
发布时间:2019-06-09

本文共 1775 字,大约阅读时间需要 5 分钟。

while语法,while循环又称为条件循环

 while 条件:

     code1

     code2

     code3

     ....

 

 

 user_db='egon'

 pwd_db='123'

 

 while True:

     inp_user=input('username>>: ')

     inp_pwd=input('password>>: ')

     if inp_user == user_db and inp_pwd == pwd_db:

         print('login successfull')

     else:

         print('user or password error')

 

 

2 while+break:break的意思是终止掉当前层的循环,.执行其他代码

 while True:

     print('1')

     print('2')

     break

     print('3')

 

 user_db='egon'

 pwd_db='123'

 

 while True:

     inp_user=input('username>>: ')

     inp_pwd=input('password>>: ')

     if inp_user == user_db and inp_pwd == pwd_db:

         print('login successfull')

         break

     else:

         print('user or password error')

 

 

 print('其他代码')

 

3 while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环

ps:记住continue一定不要加到循环体最后一步执行的代码

 n=1

 while n <= 10:

     if n == 8:

         n += 1 #n=9

         continue

     print(n)

     n+=1 #n=11

 

 

 

 while True:

     if 条件1:

         code1

         code2

         code3

         continue #无意义

     elif 条件1:

         code1

         continue #有意义

         code2

         code3

     elif 条件1:

         code1

         code2

         code3

         continue #无意义

     ....

     else:

         code1

         code2

         code3

         continue #无意义

 

 

while循环嵌套

user_db='egon'

pwd_db='123'

 

while True:

    inp_user=input('username>>: ')

    inp_pwd=input('password>>: ')

    if inp_user == user_db and inp_pwd == pwd_db:

        print('login successfull')

        while True:

            cmd=input('请输入你要执行的命令: ')

            if cmd == 'q':

                break

            print('%s 功能执行...' %cmd)

        break

    else:

        print('user or password error')

 

 

print('end....')

 

 

 

while+tag

user_db='egon'

pwd_db='123'

 

tag=True

while tag:

    inp_user=input('username>>: ')

    inp_pwd=input('password>>: ')

    if inp_user == user_db and inp_pwd == pwd_db:

        print('login successfull')

        while tag:

            cmd=input('请输入你要执行的命令: ')

            if cmd == 'q':

                tag=False

            else:

                print('%s 功能执行...' %cmd)

 

    else:

        print('user or password error')

 

 

print('end....')

 

 while+else (***)

n=1

while n < 5:

    if n == 3:

        break

    print(n)

    n+=1

else:

    print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')

转载于:https://www.cnblogs.com/wanglecn/p/9112521.html

你可能感兴趣的文章
1.1 为什么要使用lambda 表达式
查看>>
第八周作业
查看>>
bzoj 3462: DZY Loves Math II
查看>>
Minimum Depth of Binary Tree
查看>>
【python】详解map函数的用法之函数并行作用解析
查看>>
单调队列与DP
查看>>
程序猿年终总结:我看了我的这7年
查看>>
handler的使用
查看>>
如何让wp7真机调试时候保持屏幕高亮不锁屏
查看>>
网络编程
查看>>
window下python安装pip
查看>>
【剑指offer】Q14:调整数组顺序使奇数位于偶数前面
查看>>
CSS3 Flexbox轻松实现元素的水平居中和垂直居中
查看>>
iOS开发UI篇—无限轮播(新闻数据展示)
查看>>
匿名内部类
查看>>
JPA entity versioning (@Version and Optimistic Locking)
查看>>
C# 通过 HTTPModule 防范 DOS
查看>>
github push 出错:fatal: Authentication failed for 'https://github.com/ ..的解决
查看>>
shell脚本中如何插入其它脚本?
查看>>
任务状态机
查看>>