zoukankan      html  css  js  c++  java
  • 流程控制之while循环

    流程控制之while循环

    while语法,while循环又称为条件循环
    while 条件:
    code1
    code2
    code3
    ....


    user_db='egon'
    pwd_db='123'

    while True:
    inp_user=input('username>>: ') #用户输入也要加入循环里,因为如果输入用户名和密码正确,循环一次显示登陆成功就行了。
                           但是如果输入账号密码错误,在循环里,用户需要重新输入,否则就会无限循环“user or passsword error”

    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 # 循环只有一层,直接终止,此时和exit()功能一样。
    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的意思是终止掉本次循环,.直接进入下一次循环。continue下面的代码不需要再执行了。
    ps:记住continue一定不要加到循环体最后一步执行的代码
    n=1
    while n <= 10:
    if n == 8:
    n += 1 #n=9
    continue # n = 8时,if条件成立执行if下面的语句,执行完 n = 9,遇到continue,循环体下面不再执行。
    print(n)     9 != 8,if判断不成立,输出n = 9,所以循环后结果时没有8的。
    n+=1 #n=11


    while接 if..elif...else...
    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循环嵌套:while循环嵌套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': # 加入if判断语句,如果输入为q,则推出当前层循环,否则会无限输入。
    break
    print('%s 功能执行...' %cmd)
    break # 打断外面的while循环。登陆成功即退出。
    else:
    print('user or password error')


    print('end....')



    while+tag
    user_db='egon'
    pwd_db='123'

    tag=True 3这里把True赋值给tag,有一个好处就是不用两次break,只要tag = False即推出整个程序。
    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': # 输入q,程序退出。
    tag=False
    else:
    print('%s 功能执行...' %cmd)

    else:
    print('user or password error')


    print('end....')



    while+else (***)
    没有break打断的情况下,下买你的else才会执行。在whiel循环结束后执行。
    n=1
    while n < 5:
    # if n == 3:
    # break
    print(n)
    n+=1
    else:
    print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')


  • 相关阅读:
    模态对话框可能导致程序崩溃
    C++实现将字符串循环左移n个位置
    Android Gallery图片显示和文字提示及Menu 菜单
    android代码实现ScaleAnimation
    Android系统内存管理的问题
    android打电话发短信
    android开发之屏幕设置
    输入法
    黄金周
    气筒
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9104995.html
Copyright © 2011-2022 走看看