• Python 基础之循环结构 while


    一.while循环介绍

    while 循环 可以提高代码的效率,减少代码的冗余

    while 条件表达式:
        code1
        code2
    如果条件表达式成立,返回Ture,就执行其中的代码块

    1.基本语法 

    例:打印1 ~100

    #1)初始化变量i

    i = 1
    #(2)写循环判断条件
    while i<= 100:
       print(i)
    #(3)自增自减的条件
      i+=1


    #代码解析
    首先初始化i=1
    然后判断1<=100 满足返回Ture 执行代码块
    然后print(1)
    i+=1 i = i + 1 i =>2

    回到条件表达式当中进行判断 也就是回答20
    2<=100 满足 返回Ture 执行代码块
    i+=1 i= i+1 2+i =>3

    回到条件表达式当中进行判断 ,也就是回到20
    3<=100 满足 返回Ture 执行代码块
    然后print(3)
    i+=1 i = i + 1 3+1 =>4

    什么时候条件跳出?
    i=101的时候
    101<=100 不满足 返回False 不执行代码块
    循环终止。。。

    2.while的写法

    例:

    #1)第一种方法
    #1~100的累加和
    i = 1
    total = 0
    while i <= 100:
       #写上逻辑
        #print(i)
       total += 1
       i += 1
    print(total)

    代码解析:
    total += i => total = total + i => 0 + 1 => 1
    i+=1 => i = i+1 => 1+1 => 2
    2 <= 100  满 足 返回真True

    total += i => totoal = total + i => 0 + 1 + 2 => 3
    i+=1 => i = i+1 => 2+1 => 3
    3 <= 100  满足  返回真True

    total += i => totoal = total + i => 0 + 1 + 2 + 3 => 6
    i+=1 => i = i+1 => 3+1 => 4
    4 <= 100 满足 返回真True

    total += i => totoal = total + i => 0 + 1 + 2 + 3 + 4 + 5 + ...+ 100 => 5050
    dai
    i = 101 的时候
    101 <= 100 不满足 循环终止...


    # (2)死循环写法
    #whilie Ture:
        #print(1)

    i = 1
    flag = True
    total = 0
    while flag:
       total += i
       i +=1
       # 添加一个能够跳出来的条件
       if i == 101:
          flag = False
    print(total)

    #打印一行10个小星星

    #help 可以查看帮助文档
    help(print)

    i = 1
    while i <=100:
       #end = '' 默认不换行
       print("*",end="")
       i+=1
    #用一个变量输出10个小星星(配置while

    i = 0
    while i < 10:
       print("★",end="")
       i+=1

    二.关于while循环的练习题

    1.用一个变量打印出一个十个小星星

    (十个小星星塞在一个变量中,最后达成变量)
    例:
    #法一:
    a = 10
    res = "" * a
    print(res)
    print("===============")
    #法二:
    i = 0
    strvar = ''
    while i<10:
       strvar += ""
       i+=1
    print(strvar)
    print("===============")
    #法三:
    i = 0
    while i < 10:
       print("",end="")
       i+=1
    print(" ",end="=============== ")
    #法四:
    print("★★★★★★★★★★")

    2.十行十个小星星

    例:

    #法一:
    i = 0
    while i < 100:
       #打印星星
       print("",end="")
       #打印换行
       if i%10 == 9:
          print()
       i+=1
    #法二:
    print("===============")
    i = 0
    while i <= 10:
       j = 0
       while j <= 10:
          print("",end="")
          j += 1
       print(" ",end="")
       i += 1

    #法三:
    #输入数量几就输出几个几行小星星,且奇数为★,偶数为☆
    i = 0
    n = float(input("请输入星星个数:"))
    while i < n :
       if n % 2  == 0:
          j = 1
          while j <= n:
             print("", end="")
             j += 1
          print(" ", end="")
          i+=1
       else:
          j = 1
          while j <= n:
             print("", end="")
             j += 1
          print(" ", end="")
          i += 1

    #法四:
    j = 0
    n = int(input("请输入星星个数:"))
    for j in range(0,n):
       i = 0
       strvar = ''
       while i<n:
          if n % 2 == 0:
             strvar += ""
          else:
             strvar += ""
          i+=1
       print(strvar)
       j+=1

    3.十行十列隔列变色小星星

     例:

    #法一
    i = 1
    while i <= 100:
       #控制打印 隔列打印
          if i % 2==0:
             print("", end="")
          else:
             print("",end="")
          #控制换行
          if i % 10 == 0:
             print()
          i +=1
    print("==================")
    #法二:
    i = 0
    strvar = ''
    j = 0
    while j < 10:
       while i < 10:
          if i%2 == 0:
             strvar +=""
          else:
             strvar += ""
          i+=1
       print(strvar)
       j += 1

    print("==================")
    #法三:
    j = 0
    #对行的星星进行排序
    for j in range(0,10):
       i = 0
       strvar = ''
       #对列的星星进行排列
       while i < 10:
          if i%2 == 0:
             strvar +=""
          else:
             strvar += ""
          i+=1
       print(strvar)
       j+=1

    #法四
    count = 1
    while count <= 100:
       if count & 1:
          print("", end="")
       else:
          print("",end="")
       if count % 10 == 0:
          print(" ",end="")
       count +=1
    print("=======最后一种========")
    i = 0
    while i < 10:

       j = 0
       while j < 10:
          #控制打印星星的代码
          if j % 2 == 0:
             print("",end="")
          else:
             print("",end="")
          j+=1
       print()
       i += 1

    4.十行十列隔行变色小星星

    例:

    #法一

    i = 0
    while i < 10:
       j = 0
       if i%2 == 0:
          while j <= 10:
             print("",end="")
             j += 1
          print(" ",end="")
       else:
          while j <= 10:
             print("",end="")
             j += 1
          print(" ",end="")
       i += 1
    print("=========================")

    解析:
    n // 2
    0 // 2 0
    1 // 2 0

    2 // 2 1
    3 // 2 1

    0 // 4 0
    1 // 4 0
    2 // 4 0
    3 // 4 0
    4 // 4 1
    ..
    ..
    41 42 43
    任意数和n进行地板除  产生n个相同的数
    任意数 // 10 产生十个相同的数
    任意数 // 25 产生25个相同的数

    #法二
    i = 0
    while i< 100:
       #打印星星的部分
       if i // 10 %2 == 0:
          print("",end="")
       else:
          print("",end="")
       #执行部分
       if i % 10 == 9:
          print()
       i+=1

    解析:

    i = 0 1 2 3 4 5 6 7 8 9
    i // 10 0
    i = 10 11 12 13 14 15 16 17 18 19
    i //10 1
    ...........
    i // 10 2
    i // 10 得到最后的数范围是0~9
    0~9 % 2  0 或者1 正好做黑白星

    5.输入几行几列就输出几行几列隔列变色小星星

    例:

    j = 0
    m = int(input("请输入小星星的行数:"))
    n = int(input("请输入小星星的列数:"))
    #对行的星星进行排序
    for j in range(0,m):
       i = 0
       strvar = ''
       #对列的星星进行排列
       while i < n:
          if i%2 == 0:
             strvar +=""
          else:
             strvar += ""
          i+=1
       print(strvar)
       j+=1

  • 相关阅读:
    Ubuntu14.04LTS 下配置Tomcat Hadoop eclipse环境
    Ubuntu14.04LTS下 JAVA+HADOOP
    windows下libnet ARP
    windows下编译配置libnet-1.2-rc3
    windows下安装配置winpcap
    python--装饰器初阶
    python--函数进阶
    python_函数基础
    python_文件操作
    python_Set(集合)
  • 原文地址:https://www.cnblogs.com/hszstudypy/p/10803002.html
Copyright © 2020-2023  润新知