用python 打印九九乘法表,代码如下:
1、暴力运行安排:
def nine_nine():
print("1X1 = 1")
print("1X2 = 2 2X2 = 4")
print("1X3 = 3 2X3 = 6 3x3=9")
print("1X4 = 4 2X4 = 8 3x4=12 4x4=16")
print("1X5 = 5 2X5 = 10 3x5=15 4x5=20 5x5=25")
print("1X6 = 6 2X6 = 12 3x6=18 4x6=26 5x6=30 6x6=36")
print("1X7 = 7 2X7 = 14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49")
print("1X8 = 8 2X8 = 16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64")
print("1X9 = 9 2X9 = 18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81")
if __name__ == '__main__':
nine_nine()
运行结果:
2、双重循环结构安排:
def nine_nine():
for i in range(1, 10):
for j in range(1, 10):
if i >= j:
print(f"{j}X{i} = {i * j} ", end='')
else:
print()
break
if __name__ == '__main__':
nine_nine()
运行结果:
3、慢慢让它输出结果安排:
def nine_nine():
for i in range(1, 10):
for j in range(1, 10):
if i >= j:
#每隔一秒输出一个结果
time.sleep(1)
print(f"{j}X{i} = {i * j} ", end='')
else:
print()
break
if __name__ == '__main__':
nine_nine()
运行结果: