python-day02 while嵌套循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-day02 while嵌套循环相关的知识,希望对你有一定的参考价值。

while循环
1、输出打印以#组成的长方形,自己定义长和宽。
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
height = int(input("Height:"))
width  = int(input("Width:"))
num_height = 1

while num_height <= height:
    num_width = 1
    while num_width <= width:
        num_width += 1
        print("#",end="")
    num_height += 1
    print()

2、输出如下图形
   *
   * *
   * * *
   * * * *
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
num_width = 1
while num_width <= width:
    print("#"*num_width,end="\n")
    num_width += 1
3、输出2的倒叙图形:
  * * * *
  * * *
  * *
  *
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
while width > 0:
    print("#"*width,end="\n")
    width -= 1

第二种实现方式,使用嵌套循环:
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
while width > 0:
    num_width = width
    while num_width > 0:
        print("*",end="")
        num_width -= 1
    print()
    width -= 1

5、输出99乘法表
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = 1
while width <= 9:
    num_width = 1
    while num_width <= width:
        print(str(num_width)+"*"+str(width)+"="+str(num_width*width),end="\t")
        num_width += 1
    print()
    width += 1
倒叙99表
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = 9
while width > 0:
    num_width = 1
    while num_width <= width:
        print(str(num_width)+"*"+str(width)+"="+str(num_width*width),end="\t")
        num_width += 1
    print()
    width -= 1

注释:end=表示每一行的结尾,\n表示换行符,\t表示制表符




















































































以上是关于python-day02 while嵌套循环的主要内容,如果未能解决你的问题,请参考以下文章

python-day02-study

自学Python入门 (for和while)循环嵌套及用法

Python:嵌套的while循环将从第一个while开始“继续”,而不是嵌套的while

循环嵌套

while循环2(while循环嵌套)

在嵌套的 while 循环中继续