最近正在看《流畅的Python》,啃这本书花了不少时间,主要是在思考和理解,慢慢感觉到自己已经很久没有敲代码了,编程这门课最终还是要多实践、多动手,想想还是定期练练手吧。之前看到过一个Python编程题:用Python实现九九乘法表,就它了。
九九乘法表如果结果不按阶梯状打印的话,实现起来还是很简单的:
'''只获取九九乘法表的值,返回含有九九乘法表所有值的列表'''
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]
也就是说,切片表达式c[m:n]中,索引m从0开始,逐行增加1,2,3,4···;索引n从1开始,逐行增加2,3,4,5···那么下面要做的,就是把这个的逻辑用代码表达出来,代码如下
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']
至此,就完成了九九乘法表的获取和显示,在这里把过程分享给大家。