python 水仙花数、菱形、99乘法表、直角三角形
空心菱形 i = 1 while i <= 3: # 控制行数 j = 1 k = 1 while j <= 3-i: # 控制空格数量 print(" ", end="") j = j + 1 while k <= 2*i-1: if k==1 or k==2*i-1: print("*", end="") else: print(" ",end="") k = k + 1 print() i = i + 1 i = 1 while i <= 2: # 控制行数 j = 1 k = 1 while j <= i: # 控制空格数量 print(" ", end="") j = j + 1 while k <= 5-i*2: if k==1 or k== 5-i*2: print("*", end="") else: print(" ",end="") k = k + 1 print() i = i + 1
#水仙花数
i = 100 while i < 1000: a = i // 100 #求百位数 b = (i - a * 100) // 10 #求十位数 c = i % 10 #求个位数 #print(c) if a ** 3 + b ** 3 + c ** 3 == i: print(i,'是水仙花数') i += 1
# 菱形绘制 i = 1 while i <= 3: # 控制行数 j = 1 k = 1 while j <= 3-i: # 控制空格数量 print(" ", end="") j = j + 1 while k <= 2*i-1: print("*", end="") k = k + 1 print() i = i + 1 i = 1 while i <= 2: # 控制行数 j = 1 k = 1 while j <= i: # 控制空格数量 print(" ", end="") j = j + 1 while k <= 5-i*2: print("*", end="") k = k + 1 print() i = i + 1
#99乘法表 i = 1 while i <= 9: j = 1 while(j <= i): # j的大小是由i来控制的 print('%s*%s=%-3s' % (i, j, i*j), end='\t') j += 1 print("") i += 1
#直角三角形 i =1 while i<=4: print("*"*i) i=i+1 o=0 while o<=4: print("") o=o+1 i=0 while i<o: print("*",end="") i=i+1