Python基础入门篇【17】--python案例之九九乘法表

前言
作者简介:短尾巴小鳄鱼 ,一位不放弃自己的上进卷妹
💬 如果文章知识点有错误的地方,请留言指正~和大家一起学习,一起进步
💬 人生格言:昨日种种,皆成今我。今日种种,方成新我。切莫踌躇,莫停留,莫沉溺💬
如果感觉博主的文章还不错的话,还请关注、点赞、收藏三连支持一下博主哦

python案例之九九乘法表

以下仅为我个人的解题思路,其实还有很多种方法可以实现九九乘法表,大家可以尝试下不同的解决方法。

解决思路:

第一步:定义一个空字典,将我们需要的数据存入一个字典work = {},

每个键值对依次存入九九乘法表每行需要用到的数据,比如第三个键值对存入{'3': [1, 2, 3]}

第二步:提取work的所有key存入列表,方便接下来使用(因为乘法需要两个因子,观察九九乘法表每行的数据,第二个因子即为work中的key)

第三步:利用乘积 = key * value的方式,实现九九乘法表1* 1=2

第四步:全部实现九九乘法表

# coding:utf-8

# 打印一个九九乘法表
"""
 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
 ...
"""
work = {}
work['1'] = [1]
work['2'] = [1, 2]
work['3'] = [1, 2, 3]
work['4'] = [1, 2, 3, 4]
work['5'] = [1, 2, 3, 4, 5]
work['6'] = [1, 2, 3, 4, 5, 6]
work['7'] = [1, 2, 3, 4, 5, 6, 7]
work['8'] = [1, 2, 3, 4, 5, 6, 7, 8]
work['9'] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(work)
keys = list(work.keys())
print(keys)
print('{} * {} = {}'.format(work['1'][0], int(keys[0]), work['1'][0] * int(keys[0])))
print('{} * {} = {}'.format(work['2'][0], int(keys[1]), work['2'][0] * int(keys[1])), end=' ')
print('{} * {} = {}'.format(work['2'][1], int(keys[1]), work['2'][1] * int(keys[1])))
print('{} * {} = {}'.format(work['3'][0], int(keys[2]), work['3'][0] * int(keys[2])), end=' ')
print('{} * {} = {}'.format(work['3'][1], int(keys[2]), work['3'][1] * int(keys[2])), end=' ')
print('{} * {} = {}'.format(work['3'][2], int(keys[2]), work['3'][2] * int(keys[2])))
print('{} * {} = {}'.format(work['4'][0], int(keys[3]), work['4'][0] * int(keys[3])), end=' ')
print('{} * {} = {}'.format(work['4'][1], int(keys[3]), work['4'][1] * int(keys[3])), end=' ')
print('{} * {} = {}'.format(work['4'][2], int(keys[3]), work['4'][2] * int(keys[3])), end=' ')
print('{} * {} = {}'.format(work['4'][3], int(keys[3]), work['4'][3] * int(keys[3])))
print('{} * {} = {}'.format(work['5'][0], int(keys[4]), work['5'][0] * int(keys[4])), end=' ')
print('{} * {} = {}'.format(work['5'][1], int(keys[4]), work['5'][1] * int(keys[4])), end=' ')
print('{} * {} = {}'.format(work['5'][2], int(keys[4]), work['5'][2] * int(keys[4])), end=' ')
print('{} * {} = {}'.format(work['5'][3], int(keys[4]), work['5'][3] * int(keys[4])), end=' ')
print('{} * {} = {}'.format(work['5'][4], int(keys[4]), work['5'][4] * int(keys[4])))
print('{} * {} = {}'.format(work['6'][0], int(keys[5]), work['6'][0] * int(keys[5])), end=' ')
print('{} * {} = {}'.format(work['6'][1], int(keys[5]), work['6'][1] * int(keys[5])), end=' ')
print('{} * {} = {}'.format(work['6'][2], int(keys[5]), work['6'][2] * int(keys[5])), end=' ')
print('{} * {} = {}'.format(work['6'][3], int(keys[5]), work['6'][3] * int(keys[5])), end=' ')
print('{} * {} = {}'.format(work['6'][4], int(keys[5]), work['6'][4] * int(keys[5])), end=' ')
print('{} * {} = {}'.format(work['6'][5], int(keys[5]), work['6'][5] * int(keys[5])))
print('{} * {} = {}'.format(work['7'][0], int(keys[6]), work['7'][0] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][1], int(keys[6]), work['7'][1] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][2], int(keys[6]), work['7'][2] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][3], int(keys[6]), work['7'][3] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][4], int(keys[6]), work['7'][4] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][5], int(keys[6]), work['7'][5] * int(keys[6])), end=' ')
print('{} * {} = {}'.format(work['7'][6], int(keys[6]), work['7'][6] * int(keys[6])))
print('{} * {} = {}'.format(work['8'][0], int(keys[7]), work['8'][0] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][1], int(keys[7]), work['8'][1] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][2], int(keys[7]), work['8'][2] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][3], int(keys[7]), work['8'][3] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][4], int(keys[7]), work['8'][4] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][5], int(keys[7]), work['8'][5] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][6], int(keys[7]), work['8'][6] * int(keys[7])), end=' ')
print('{} * {} = {}'.format(work['8'][7], int(keys[7]), work['8'][7] * int(keys[7])))
print('{} * {} = {}'.format(work['9'][0], int(keys[8]), work['9'][0] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][1], int(keys[8]), work['9'][1] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][2], int(keys[8]), work['9'][2] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][3], int(keys[8]), work['9'][3] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][4], int(keys[8]), work['9'][4] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][5], int(keys[8]), work['9'][5] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][6], int(keys[8]), work['9'][6] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][7], int(keys[8]), work['9'][7] * int(keys[8])), end=' ')
print('{} * {} = {}'.format(work['9'][8], int(keys[8]), work['9'][8] * int(keys[8])))

运行结果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-91uBPUrl-1658135915777)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20220718171747973.png)]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值