week1:一些小程序 if判断 while 循环(下)

2.5两层while循环

1.换行输出一些数据

num1 = 0

while num1<=5:
    print(num1,end="_")
    num2 = 0
    while num2<=7:
        print(num2,end="-")
        num2+=1
        
    num1+=1
    print() #  print(end="\n")

输出结果为:#0_0-1-2-3-4-5-6-7-

                    #1_0-1-2-3-4-5-6-7-

2.使用#号输出一个长方形,用户可以指定高和宽。

height = int(input("Height:"))  # 用户输入一个高度
width = int(input("width:"))   # 用户输入一个宽度

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

3使用#号输出一个正方形,用户可以指定高和宽。

####
####
####
####

width = int(input("width:"))

num_width = 1

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

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

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

num_width = 1
while num_width<=width:
    print("#", end="")
    num_width +=1
num2=2
while num2>0:
	num=2
	while num>0:
		print("#",end="")
		num-=1
	print()
	num2-=1

4.如何输出一个如下的直接三角形,用户指定输出行数,(如果上下反转,又如何实现?)

*

**

***

****

line=5
while line>0:
	tmp=line
	while tmp>0:
		print("*",end="")
		tmp=tmp-1
	print()
	line-=1
	

5.输出一个九九乘法表

first=9
#second=9
while first>0:
	sec=1
	while sec<=first:
		print(str(sec)+"*"+str(first)+"=" ,sec*first,end="\t")#\t 是制表符 
		#print(str(sec)+"*"+str(first)+"=" +str(sec*first),end="\t")#\t 是制表符 
		#print(sec,"*",first,"=" ,sec*first,end="  ")
		sec+=1
	print()
	first-=1

6.break语句

num = 1
while num <= 10:
    num += 1
    if num == 5:
        break
    print(num)
else:
    print("This is else statement")

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值