Python打印换行九九乘法表的代码
时间: 2023-11-19 14:09:47 浏览: 63
可以使用嵌套循环来实现九九乘法表的打印,并且在每一行结束后使用print()函数进行换行。下面是代码示例:
```python
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
```
运行以上代码,就可以打印出换行的九九乘法表了。
相关问题
python编程打印九九乘法表
在Python中,打印九九乘法表是一个常见的编程练习,通常使用嵌套循环来实现。以下是用Python编写的一个简单示例:
```python
# 定义一个函数来打印九九乘法表
def print_multiplication_table():
for i in range(1, 10):
# 内层循环从1到i,用于生成乘积
for j in range(1, i+1):
# 计算乘积并打印
product = i * j
print(f"{i} * {j} = {product}", end="\t") # 使用'\t'让数字之间用制表符分隔
# 换行开始新的一行
print() # 这里结束内层循环并打印一个换行符
# 调用函数打印九九乘法表
print_multiplication_table()
```
当你运行这段代码时,它会输出九九乘法表的全部内容。
python打印倒置九九乘法表
可以使用嵌套循环来打印倒置九九乘法表,代码如下:
```python
for i in range(9, 0, -1): # 外层循环,倒序输出行数
for j in range(9, i-1, -1): # 内层循环,倒序输出列数
print('{} x {} = {:2d}'.format(j, i, j*i), end=' ')
print() # 打印完一行后换行
```
输出结果如下:
```
9 x 9 = 81 8 x 9 = 72 7 x 9 = 63 6 x 9 = 54 5 x 9 = 45 4 x 9 = 36 3 x 9 = 27 2 x 9 = 18 1 x 9 = 9
9 x 8 = 72 8 x 8 = 64 7 x 8 = 56 6 x 8 = 48 5 x 8 = 40 4 x 8 = 32 3 x 8 = 24 2 x 8 = 16 1 x 8 = 8
9 x 7 = 63 8 x 7 = 56 7 x 7 = 49 6 x 7 = 42 5 x 7 = 35 4 x 7 = 28 3 x 7 = 21 2 x 7 = 14 1 x 7 = 7
9 x 6 = 54 8 x 6 = 48 7 x 6 = 42 6 x 6 = 36 5 x 6 = 30 4 x 6 = 24 3 x 6 = 18 2 x 6 = 12 1 x 6 = 6
9 x 5 = 45 8 x 5 = 40 7 x 5 = 35 6 x 5 = 30 5 x 5 = 25 4 x 5 = 20 3 x 5 = 15 2 x 5 = 10 1 x 5 = 5
9 x 4 = 36 8 x 4 = 32 7 x 4 = 28 6 x 4 = 24 5 x 4 = 20 4 x 4 = 16 3 x 4 = 12 2 x 4 = 8 1 x 4 = 4
9 x 3 = 27 8 x 3 = 24 7 x 3 = 21 6 x 3 = 18 5 x 3 = 15 4 x 3 = 12 3 x 3 = 9 2 x 3 = 6 1 x 3 = 3
9 x 2 = 18 8 x 2 = 16 7 x 2 = 14 6 x 2 = 12 5 x 2 = 10 4 x 2 = 8 3 x 2 = 6 2 x 2 = 4 1 x 2 = 2
9 x 1 = 9 8 x 1 = 8 7 x 1 = 7 6 x 1 = 6 5 x 1 = 5 4 x 1 = 4 3 x 1 = 3 2 x 1 = 2 1 x 1 = 1
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)