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")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章