python while 循环打印九九乘法表
方向一
i = 1
while i <= 9:
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i += 1
思路:先写出列,从一到九,再写出列,每次行=列的时候换行,行数增加一,所以里面的while循环是 j <= i,当行数小于列数时,执行换行代码,进行下一轮的循环,end = ‘’ 表示不换行继续往下写
方向一打印结果示例
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
方向二
i = 1
while i <= 9:
k = 1
while k <= 9 - i:
print(' ',end = ' ')
k += 1
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
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
方向三
i = 9
while i >= 1:
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i -= 1
思路:方向三中的i是从9到1,然后再是把i一个一个往下减
方向三打印结果示例
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
方向四
i = 9
while i >= 1:
k = 1
while k <= 9-i:
print(' ', end = ' ')
k += 1
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),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
python while 循环打印九九乘法表的更多相关文章
- 写一个方法,用一个for循环打印九九乘法表
public class MultiplicationTable { /** * @description 写一个方法,用一个for循环打印九九乘法表 * @author wangkun * ...
- Java-for循环打印九九乘法表
Java打印九九乘法表 public class forDemo04 { public static void main(String[] args) { //练习3:打印九九乘法表 /* 1*1=1 ...
- for循环打印九九乘法表
学习目标: 熟练掌握 for 循环的使用 例题: 需求:打印九九乘法表 代码如下: // 九九乘法表 // row 为行,col为列 for(int row = 1; row < 10; row ...
- python脚本7_打印九九乘法表
#打印九九乘法表 for i in range(1,10): s = "" for j in range(1,i+1): s += str(j) + '*' + str(i) + ...
- 用JS的for循环打印九九乘法表
需要使用两个for循环嵌套,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- lua学习之循环打印九九乘法表
--第4题 输出99乘法表 function PrintMulitiplyTable() , do local res = {} local str = "" , i do res ...
- python3:使用for循环打印九九乘法表
for i in range(1, 10): for j in range(1, i + 1): print(j, '*', i, '=', i * j, end=" ") #en ...
- 用for循环打印九九乘法表(for嵌套循环)
package com.Summer_0416.cn; /** * @author Summer * */ public class Test_Method10 { public static voi ...
- JavaScript-双层for循环打印九九乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- 08-js流程控制、循环、元素操作
# js流程控制 > 流程控制用于基于不同的条件来执行不同的动作. ### if语句 >if... else ... >if ... else if ... else... > ...
- linux 调整内核优化
所谓内核优化,主要是在 linux 中针对业务服务应用而进行的系统内核参数优化,优化并无特殊的 标准,下面以常见生产环境 linux 的内核优化为例讲解,仅供大家参考: 内核调优 #vi /etc/s ...
- 解决SVN异常 cleanup failed
winndows上偶尔使用SVN的时候就会整出一些有的没的问题,比如"cleanup failed to process the following paths previous opera ...
- 使用python3搭建Linux-mariadb主从架构
环境准备两台: 192.168.193.90 master 192.168.193.91 slave 需要Linux装python环境: https://www.cnblogs.com/kingzhe ...
- 【转】SIP协议 会话发起协议
转自:https://www.cnblogs.com/gardenofhu/p/7299963.html 会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协 ...
- pwd 显示当前所在的工作路径
1.功能说明 pwd命令是“print working directory ”首字母缩写,显示当前目录的绝对路径. 2.语法格式 pwd [option] pwd 选项 3.命令参数 参数 参数说明 ...
- css3 first-of-type选择器以及css3选择器中:first-child与:first-of-type的区别
CSS3 first-of-type选择器 “:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子 ...
- phpstorm 生产php pojo类
一. 修改Generate POJO.groovy文件 改为 import com.intellij.database.model.DasTable import com.intellij.datab ...
- UVa 1009 Sharing Chocolate (数位dp)
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- Linux下安装Harbor 1.8.0 仓库的安装和使用(亲测)
根据Harbor官方描述: Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distri ...