python 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表示制表符

转载于:https://www.cnblogs.com/cuishuai/p/7242558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值