# 通过Python实现九九乘法表

4 篇文章 0 订阅

## 一、只获取九九乘法表的值

'''只获取九九乘法表的值，返回含有九九乘法表所有值的列表'''

a = 1
b = 1
c = []
while True:
if a < 9:
if b < 9:
d = a * b
c.append(d)
b += 1
continue
if b == 9:
d = a * b
c.append(d)
a += 1
b = a
continue
if a == 9:
d = a * b
c.append(d)
break

print(c)

'''以下为返回的内容'''

[1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 6, 8, 10, 12, 14, 16, 18, 9, 12, 15, 18, 21, 24, 27, 16, 20, 24, 28, 32, 36, 25, 30, 35, 40, 45, 36, 42, 48, 54, 49, 56, 63, 64, 72, 81]

## 二、 同时获取九九乘法表的公示和值

'''只需要在上面的基础上，把a*b公式和值以f字符串的形式赋值给d，就可以获取九九乘法表的公式和值'''

a = 1
b = 1
c = []
while True:
if a < 9:
if b < 9:
d = f'{a} * {b} = {a*b}'
c.append(d)
b += 1
continue
if b == 9:
d = f'{a} * {b} = {a*b}'
c.append(d)
a += 1
b = a
continue
if a == 9:
d = f'{a} * {b} = {a * b}'
c.append(d)
break

print(c)

'''以下为返回的内容'''

['1 * 1 = 1', '1 * 2 = 2', '1 * 3 = 3', '1 * 4 = 4', '1 * 5 = 5', '1 * 6 = 6', '1 * 7 = 7', '1 * 8 = 8', '1 * 9 = 9', '2 * 2 = 4', '2 * 3 = 6', '2 * 4 = 8', '2 * 5 = 10', '2 * 6 = 12', '2 * 7 = 14', '2 * 8 = 16', '2 * 9 = 18', '3 * 3 = 9', '3 * 4 = 12', '3 * 5 = 15', '3 * 6 = 18', '3 * 7 = 21', '3 * 8 = 24', '3 * 9 = 27', '4 * 4 = 16', '4 * 5 = 20', '4 * 6 = 24', '4 * 7 = 28', '4 * 8 = 32', '4 * 9 = 36', '5 * 5 = 25', '5 * 6 = 30', '5 * 7 = 35', '5 * 8 = 40', '5 * 9 = 45', '6 * 6 = 36', '6 * 7 = 42', '6 * 8 = 48', '6 * 9 = 54', '7 * 7 = 49', '7 * 8 = 56', '7 * 9 = 63', '8 * 8 = 64', '8 * 9 = 72', '9 * 9 = 81']

## 三、获取九九乘法表的公式和值并阶梯状显示

### 第一步，修改逻辑

def cf():
a = 1
b = 1
c = []
while True:
if a < b:
d = f'{a} * {b} = {a*b}'
c.append(d)
a += 1
continue
if a == b:
if b < 9:
d = f'{a} * {b} = {a*b}'
c.append(d)
b += 1
a = 1
continue
if b == 9:
d = f'{a} * {b} = {a*b}'
c.append(d)
print(c)
print(len(c))
break
return c

cf()

'''这样，最终得出的列表是这样'''

['1 * 1 = 1', '1 * 2 = 2', '2 * 2 = 4', '1 * 3 = 3', '2 * 3 = 6', '3 * 3 = 9', '1 * 4 = 4', '2 * 4 = 8', '3 * 4 = 12', '4 * 4 = 16', '1 * 5 = 5', '2 * 5 = 10', '3 * 5 = 15', '4 * 5 = 20', '5 * 5 = 25', '1 * 6 = 6', '2 * 6 = 12', '3 * 6 = 18', '4 * 6 = 24', '5 * 6 = 30', '6 * 6 = 36', '1 * 7 = 7', '2 * 7 = 14', '3 * 7 = 21', '4 * 7 = 28', '5 * 7 = 35', '6 * 7 = 42', '7 * 7 = 49', '1 * 8 = 8', '2 * 8 = 16', '3 * 8 = 24', '4 * 8 = 32', '5 * 8 = 40', '6 * 8 = 48', '7 * 8 = 56', '8 * 8 = 64', '1 * 9 = 9', '2 * 9 = 18', '3 * 9 = 27', '4 * 9 = 36', '5 * 9 = 45', '6 * 9 = 54', '7 * 9 = 63', '8 * 9 = 72', '9 * 9 = 81']

### 第二步，分析规律

c[0:1]

c[1:3]

c[3:6]

c[6:10]

c[10:15]

c[15:21]

c[21:28]

c[28:36]

c[36:45]

def cf2(c):
a = 0
b = 1
m = 1
n = 2
g = c[a:b]
print(g)
while True:
if b < 45:
a = a + m
b = b + n
g = c[a:b]
print(g)
m += 1
n += 1
continue
else:
break

z = cf()
cf2(z)

'''执行代码以后，可以得到如下显示'''

['1 * 1 = 1']
['1 * 2 = 2', '2 * 2 = 4']
['1 * 3 = 3', '2 * 3 = 6', '3 * 3 = 9']
['1 * 4 = 4', '2 * 4 = 8', '3 * 4 = 12', '4 * 4 = 16']
['1 * 5 = 5', '2 * 5 = 10', '3 * 5 = 15', '4 * 5 = 20', '5 * 5 = 25']
['1 * 6 = 6', '2 * 6 = 12', '3 * 6 = 18', '4 * 6 = 24', '5 * 6 = 30', '6 * 6 = 36']
['1 * 7 = 7', '2 * 7 = 14', '3 * 7 = 21', '4 * 7 = 28', '5 * 7 = 35', '6 * 7 = 42', '7 * 7 = 49']
['1 * 8 = 8', '2 * 8 = 16', '3 * 8 = 24', '4 * 8 = 32', '5 * 8 = 40', '6 * 8 = 48', '7 * 8 = 56', '8 * 8 = 64']
['1 * 9 = 9', '2 * 9 = 18', '3 * 9 = 27', '4 * 9 = 36', '5 * 9 = 45', '6 * 9 = 54', '7 * 9 = 63', '8 * 9 = 72', '9 * 9 = 81']



• 1
点赞
• 15
收藏
觉得还不错? 一键收藏
• 0
评论
10-12
02-20 5151
08-23 558
06-05 2万+
10-20
01-27
06-08
12-17 4万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。