(显示一个金字塔)编写程序提示用户输人一个在1到15之间的整数,然后显示一个金字塔,示例运行如下所示。
a = int(input("请输入你要打印的行数:"))
for i in range(1,a + 1):for k in range(a , 1 ,-1):if k <= i:print(k,end = " ")else:print(" ",end = " ")for j in range(1,a + 1):if j <= i:print(j,end = " ")print("")
(使用循环显示四种模式)使用嵌套循环在四个独立的程序中显示下面四种模式。
A
a = 6
i = 1
for i in range(1,a+1):for j in range(1 , i + 1):print(j,end = " ")print()i += 1B
a = 6
while a > 0:for i in range(a):print(i+1,end = " ")print()a -= 1C
for i in range(1,7):for j in range(6,0,-1):if j <= i:print("%s " %j,end="")else:print(" ",end="")print()D
for i in range(6,0,-1):a = 6 - iprint(" " *a,end="")for j in range(1,7):if j <= i:print("%s " %j,end="")print("")
(在金字塔模式中显示数字)编写一个嵌套for 循环来显示下面的输出。
layer = int(input("请输入你要打印的行数:"))
for i in range(0,layer):for k in range(layer - i , 0 , -1):print(" ",end = "\t")for j in range(1,i + 1):print(2 ** (j - 1),end = "\t")for k in range(i + 1,0 ,-1):print(2 ** (k - 1),end = "\t")print()