• while循环


    基础循环

      while 条件:

      缩进 循环体

    i=1
    j=1
    while i in range(1,10):
    while j in range(1,i+1):
    print(j,"*",i,"=",j*i,end=' ')
    j += 1
    print()
    i += 1
    j = 1
    循环标识位:通过对标识位重新赋值退出循环
    flag = True
    while flag:#标识位为真,循环进行
    print(flag)
    flag = False #标识位重新赋值为假,结束循环

    break关键字

      用于中断循环,通常在满足某种条件时使用

    while flag:#标识位为真,循环进行
    print(flag)
    flag = False #标识位重新赋值为假,结束循环

    continue关键字

      用于结束本次循环,仅仅跳出本次

    i = 0
    while i in range(10):
    if i == 5:
    continue #当i=5时结束循环
    print(i)
    i += 1
    --------
    嵌套循环跳出
    flag = False
    for i in range(10):
    if i < 5 :
    continue
    print(i)
    for j in range(10):
    if j > 5:
    flag = True
    break #跳出当前循环
    print("Leve %s"%j)
    if flag == True:
    break #跳出外层循环

    while else使用

    1,while 条件满足不执行else情况:使用关键字break中止循环

    i = 0
    while i < 10:
    print(i)
    i += 1
    break
    else:
    print("Game over !")

    2,while条件不满足执行else:

    i = 0
    while i > 10:
    print(i)
    i += 1
    break
    else:
    print("Game over !")

    3,while条件满足,while和else均执行:

    i = 0
    while i < 10:
    print(i)
    i += 1
    else:
    print("Game over !")

    九九乘法表

    while版

    i = 1
    j = 1
    while i in range(1,10):
    while j in range(1,i+1):
    print(j,"*",i,"=",i*j,end=" ")
    j += 1
    print()
    i += 1
    j = 1
    for循环版:
    for i in range(1,10):
    for j in range(1,i+1):
    print(j,"*",i,"=",j*i,end=" ")
    j += 1
    i += 1
    print()
    -------------------
    登录设计
    _user ="zhang"
    _passwd = "123456"
    counter = 0
    while counter <3:
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _passwd :
    print("Welcome %s login...." % _user)
    break #跳出,中断
    else:
    print("Invalid username or password !")
    counter += 1
    if counter == 3:
    keep_going_choice = input("还想玩么?[y/n]")
    if keep_going_choice == "y":
    counter = 0

    else:
    print("退出。")





  • 相关阅读:
    浅析人脸检测之Haar分类器方法
    python调用网络摄像机
    jvm常用优化方案和方法
    jvm优化-垃圾收集器的类型
    jvm调优-垃圾回收算法
    JVM调优-java虚拟机内存模型及参数设置
    java NIO-Buffer
    java NIO-Channel
    java权限设置文件-java.policy
    java安全-安全管理器
  • 原文地址:https://www.cnblogs.com/zzzhao/p/11335283.html
Copyright © 2020-2023  润新知