初学者,仅供学习交流
1.(for in)循环结构实现九九乘法表的五种效果
代码如下:
while True:
key_word=input("请输入想要显示的九九乘法表效果(1.◢ 2.◣ 3.◥ 4.◤ 5.◆ 0.退出):")
while int(key_word)==1:
for y in range(1,10):
for k in range(1,10-y):
print(end=" ")#设置缩进值
for x in range(y,0,-1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◢型九九乘法表
print("")
break
while int(key_word)==2:
for y in range(1,10):
for x in range(1,y+1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◣型九九乘法表
print("")
break
while int(key_word)==3:
for y in range(9,0,-1):
for k in range(y,9):
print(end=" ")#设置缩进值
for x in range(y,0,-1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◥型九九乘法表
print("")
break
while int(key_word)==4:
for y in range(9,0,-1):
for x in range(1,y+1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◤型九九乘法表
print("")
break
while int(key_word)==5:#◆型九九乘法表,即上述四种类型合并
for y in range(1,10):
for k in range(1,10-y):
print(end=" ")#设置缩进值
for x in range(y,0,-1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◢型九九乘法表
for x in range(1,y+1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◣型九九乘法表
print("")
print("")
for y in range(9,0,-1):
for k in range(y,9):
print(end=" ")#设置缩进值
for x in range(y,0,-1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◥型九九乘法表
for x in range(1,y+1):
print("{}×{}={:<4}".format(x,y,x*y),end="")#◤型九九乘法表
print("")
break
if int(key_word)==0:#退出人机交互端口
break
2.(while)循环结构实现九九乘法表的五种效果
代码如下:
while True:
key_word=input("请输入想要显示的九九乘法表效果(1.◢ 2.◣ 3.◥ 4.◤ 5.◆ 0.退出):")
while int(key_word)==1:
y=1
while y<10:
k=1
while k<10-y:
print(end=" ")#设置缩进值
k+=1
x=y
while x>0:
print("{}×{}={:<4}".format(x,y,x*y),end="")#◢型九九乘法表
x-=1
y+=1
print("")
break
while int(key_word)==2:
y=1
while y<10:
x=1
while x
print("{}×{}={:<4}".format(x,y,x*y),end="")#◣型九九乘法表
x+=1
y+=1
print("")
break
while int(key_word)==3:
y=9
while y>0:
k=y
while k<9:
print(end=" ")#设置缩进值
k+=1
x=y
while x>0:
print("{}×{}={:<4}".format(x,y,x*y),end="")#◥型九九乘法表
x-=1
y-=1
print("")
break
while int(key_word)==4:
y=9
while y>0:
x=1
while x
print("{}×{}={:<4}".format(x,y,x*y),end="")#◤型九九乘法表
x+=1
y-=1
print("")
break
while int(key_word)==5:#◆型九九乘法表,即上述四种类型合并
y=1
while y<10:
k=1
while k<10-y:
print(end=" ")#设置缩进值
k+=1
x=y
while x>0:
print("{}×{}={:<4}".format(x,y,x*y),end="")#◢型九九乘法表
x-=1
x=1
while x
print("{}×{}={:<4}".format(x,y,x*y),end="")#◣型九九乘法表
x+=1
y+=1
print("")
print("")
y=9
while y>0:
k=y
while k<9:
print(end=" ")#设置缩进值
k+=1
x=y
while x>0:
print("{}×{}={:<4}".format(x,y,x*y),end="")#◥型九九乘法表
x-=1
x=1
while x
print("{}×{}={:<4}".format(x,y,x*y),end="")#◤型九九乘法表
x+=1
y-=1
print("")
break
if int(key_word)==0:#退出人机交互端口
break
3.五种效果图
欢迎大家提出改进意见
阅读更多