初学Python-day7 案例(乘法口诀 已更新!!)
案例::(乘法口诀)
用for循环做乘法口诀:
1 # 第一种
2 for i in range(1, 10):
3 for j in range(1, i + 1):
4 print('{} * {} ='.format(j, i), j*i, end=' ')
5 print()
运行结果:
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 #第二种
2 for i in range(9, 0, -1):
3 for j in range(1, i + 1):
4 print('{} * {} ='.format(j, i), j*i, end=' ')
5 print()
运行结果:
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 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 2 = 2 2 * 2 = 4
1 * 1 = 1
1 # 第三种
2
for i in range(9, 0, -1):
for k in range(0, 9 - i):
print(' ', end='')
for j in range(i, 0, -1):
print('{} * {} ='.format(j, i), j*i, end=' ')
print()
运行结果:
9 * 9 = 81 8 * 9 = 72 7 * 9 = 63 6 * 9 = 54 5 * 9 = 45 4 * 9 = 36 3 * 9 = 27 2 * 9 = 18 1 * 9 = 9
8 * 8 = 64 7 * 8 = 56 6 * 8 = 48 5 * 8 = 40 4 * 8 = 32 3 * 8 = 24 2 * 8 = 16 1 * 8 = 8
7 * 7 = 49 6 * 7 = 42 5 * 7 = 35 4 * 7 = 28 3 * 7 = 21 2 * 7 = 14 1 * 7 = 7
6 * 6 = 36 5 * 6 = 30 4 * 6 = 24 3 * 6 = 18 2 * 6 = 12 1 * 6 = 6
5 * 5 = 25 4 * 5 = 20 3 * 5 = 15 2 * 5 = 10 1 * 5 = 5
4 * 4 = 16 3 * 4 = 12 2 * 4 = 8 1 * 4 = 4
3 * 3 = 9 2 * 3 = 6 1 * 3 = 3
2 * 2 = 4 1 * 2 = 2
1 * 1 = 1
1 # 第四种
2 for i in range(1, 10):
3 for k in range(0, 9 - i):
4 print(' ', end='')
5 for j in range(i, 0, -1):
6 print('{} * {} ='.format(j, i), j*i, end=' ')
7 print()
运行结果:
1 * 1 = 1
2 * 2 = 4 1 * 2 = 2
3 * 3 = 9 2 * 3 = 6 1 * 3 = 3
4 * 4 = 16 3 * 4 = 12 2 * 4 = 8 1 * 4 = 4
5 * 5 = 25 4 * 5 = 20 3 * 5 = 15 2 * 5 = 10 1 * 5 = 5
6 * 6 = 36 5 * 6 = 30 4 * 6 = 24 3 * 6 = 18 2 * 6 = 12 1 * 6 = 6
7 * 7 = 49 6 * 7 = 42 5 * 7 = 35 4 * 7 = 28 3 * 7 = 21 2 * 7 = 14 1 * 7 = 7
8 * 8 = 64 7 * 8 = 56 6 * 8 = 48 5 * 8 = 40 4 * 8 = 32 3 * 8 = 24 2 * 8 = 16 1 * 8 = 8
9 * 9 = 81 8 * 9 = 72 7 * 9 = 63 6 * 9 = 54 5 * 9 = 45 4 * 9 = 36 3 * 9 = 27 2 * 9 = 18 1 * 9 = 9
用while循环做乘法口诀:
1 # 第一种
2 i = 1
3 while i < 10:
4 j = 1
5 while j < i + 1:
6 print('{} * {} ='.format(j, i), j*i, end=' ')
7 j += 1
8 print()
9 i += 1
运行结果:
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 Process finished with exit code 0
1 #第二种
2 i = 1
3 while i < 10:
4 k = 1
5 while k < 10 - i:
6 print(' ', end=' ')
7 k += 1
8 j = i
9 while j > 0:
10 print('{} * {} ='.format(j, i), j*i, end=' ')
11 j -= 1
12 print()
13 i += 1
运行结果:
1 * 1 = 1
2 * 2 = 4 1 * 2 = 2
3 * 3 = 9 2 * 3 = 6 1 * 3 = 3
4 * 4 = 16 3 * 4 = 12 2 * 4 = 8 1 * 4 = 4
5 * 5 = 25 4 * 5 = 20 3 * 5 = 15 2 * 5 = 10 1 * 5 = 5
6 * 6 = 36 5 * 6 = 30 4 * 6 = 24 3 * 6 = 18 2 * 6 = 12 1 * 6 = 6
7 * 7 = 49 6 * 7 = 42 5 * 7 = 35 4 * 7 = 28 3 * 7 = 21 2 * 7 = 14 1 * 7 = 7
8 * 8 = 64 7 * 8 = 56 6 * 8 = 48 5 * 8 = 40 4 * 8 = 32 3 * 8 = 24 2 * 8 = 16 1 * 8 = 8
9 * 9 = 81 8 * 9 = 72 7 * 9 = 63 6 * 9 = 54 5 * 9 = 45 4 * 9 = 36 3 * 9 = 27 2 * 9 = 18 1 * 9 = 9
#第三种
i = 9
while i > 0:
j = 1
while j < i + 1:
print('{} * {} ='.format(j, i), j*i, end=' ')
j += 1
print()
i -= 1
运行结果:
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 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 2 = 2 2 * 2 = 4
1 * 1 = 1 Process finished with exit code 0
1 # 第四种
2 i = 9
3 while i > 0:
4 k = 1
5 while k < 10 - i:
6 print(' ', end=' ')
7 k += 1
8 j = i
9 while j > 0:
10 print('{} * {} ='.format(j, i), j*i, end=' ')
11 j -= 1
12 print()
13 i -= 1
运行结果:
注意:图形样式不太符合,会在更新。
9 * 9 = 81 8 * 9 = 72 7 * 9 = 63 6 * 9 = 54 5 * 9 = 45 4 * 9 = 36 3 * 9 = 27 2 * 9 = 18 1 * 9 = 9
8 * 8 = 64 7 * 8 = 56 6 * 8 = 48 5 * 8 = 40 4 * 8 = 32 3 * 8 = 24 2 * 8 = 16 1 * 8 = 8
7 * 7 = 49 6 * 7 = 42 5 * 7 = 35 4 * 7 = 28 3 * 7 = 21 2 * 7 = 14 1 * 7 = 7
6 * 6 = 36 5 * 6 = 30 4 * 6 = 24 3 * 6 = 18 2 * 6 = 12 1 * 6 = 6
5 * 5 = 25 4 * 5 = 20 3 * 5 = 15 2 * 5 = 10 1 * 5 = 5
4 * 4 = 16 3 * 4 = 12 2 * 4 = 8 1 * 4 = 4
3 * 3 = 9 2 * 3 = 6 1 * 3 = 3
2 * 2 = 4 1 * 2 = 2
1 * 1 = 1
初学Python-day7 案例(乘法口诀 已更新!!)的更多相关文章
- 初学Python “登录”案例 更新!!
更新内容:添加了登录次数,如果超过限制的次数,则提示账户被锁定,去某邮箱申请解锁账户! 此次仅把登录系统更新之后源代码放到这里,不在共享源文件在网盘了! 1 ''' 2 登录界面 3 ''' 4 5 ...
- 用python打印99乘法口诀表
代码如下 #!/usr/bin/env python # encoding: utf-8 __author__ = 'Nicholas.Cage' i = 1 j = 1 while i <= ...
- Python 打印99乘法口诀表
import string for x in xrange(1,10): for y in xrange(1,x+1): print string.ljust("%d*%d = " ...
- python 9*9乘法口诀表
# -*- coding: utf-8 -*- # __author__ = 'Carry' for i in range(1, 10): for j in range(1, i + 1): prin ...
- python 9*9乘法口诀 猜数字游戏
- Python 9X9乘法口诀表
#乘法口诀 for i in range(1,10): for j in range(1,i+1): print ( j,'*',i,'=',j*i,'',' ',end='') print(end= ...
- 关于python的多行注释,启动新浏览器,循环语句乘法口诀
1,提问:如何将python写的多行代码改写成注释,进行写下一段代码?这样可以在多个脚本中写东西? 回答:百度了一下,还真有 选中所要注释的代码 CTRL + / 然后所选的代码前面都会出现#,编程 ...
- Python 乘法口诀表
环境 Anaconda3 Python 3.6, Window 64bit 目的 输出9*9 乘法口诀表 代码 # -*- coding: utf-8 -*- ''' 1*1=1 2*1=2 2*2= ...
- python的乘法口诀表
python的乘法口诀表 python的乘法口诀表 用python来写一个脚本,使得这个脚本在运行后自动输出乘法口诀表. pyton的脚本如下: #!/usr/bin/env python #codi ...
随机推荐
- uni-app 入门小白纯徒手编写组件 hello-popup
我的需求是:弹出框顶部有 title,底部有确认和取消按钮.这两部分固定,中间部分 content 的高度随自身内容会动态增长,但是它最大高度不能超过父节点 bg 的 80%,而父节点 bg 的高度也 ...
- Spring Boot 入门系列(二十四)多环境配置,3分钟搞定!
之前讲过Spring Boot 的系统配置和自定义配置,实现了按照实际项目的要求配置系统的相关熟悉.但是,在实际项目开发过程中,需要面对不同的环境,例如:开发环境,测试环境,生产环境.各个环境的数据库 ...
- Leetcode 146. LRU 缓存机制
前言 缓存是一种提高数据读取性能的技术,在计算机中cpu和主内存之间读取数据存在差异,CPU和主内存之间有CPU缓存,而且在内存和硬盘有内存缓存.当主存容量远大于CPU缓存,或磁盘容量远大于主存时,哪 ...
- Django的模板文件的路径设置
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS' ...
- tar 命令简介
tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...
- openswan发送状态机分析
openswan发送状态机分析 1. 函数调用关系 2. 函数说明 如果按用户空间.内核空间划分的话,此部分代码更多是运行在内核空间的. 2.1 ipsec_tunnel_init_devices() ...
- 为老的vueCli项目添加vite支持
1.前言 接手公司的某个项目已经两年了,现在每次启动项目都接近1分钟,hmr也要好几秒的时间,but vite2发布之后就看到了曙光,但是一直没有动手进行升级,昨天终于忍不住了,升级之后几秒钟就完成了 ...
- 面试HashMap你都扛不住,还想拿到offer?
当我们面试Java开发岗位时,面试官问的频率出现最多的问题,就是这个HashMap,不管是传统型公司还是互联公司,HashMap是必问的,所以作者爆肝整理了HashMap的23个问题以及答案,请查收! ...
- 用python的pandas读取excel文件中的数据
一.读取Excel文件 使用pandas的read_excel()方法,可通过文件路径直接读取.注意到,在一个excel文件中有多个sheet,因此,对excel文件的读取实际上是读取指定文件.并 ...
- 238 day02_Collection、泛型
day02[Collection.泛型] 主要内容 Collection集合 迭代器 增强for 泛型 教学目标 [ ] 能够说出集合与数组的区别 [ ] 说出Collection集合的常用功能 [ ...