python求九九乘法表之和
时间: 2023-05-20 18:02:58 浏览: 84
可以使用嵌套循环来实现九九乘法表,然后再计算每个数字的和。以下是代码示例:
```python
sum = 0
for i in range(1, 10):
for j in range(1, i+1):
print("%d*%d=%d" % (j, i, i*j), end='\t')
sum += i*j
print()
print("九九乘法表之和为:", sum)
```
输出结果:
```
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
九九乘法表之和为: 2025
```
相关问题
python 用python编写九九乘法表
当然可以,以下是用Python编写九九乘法表的代码:
```
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
```
这段代码使用了两个for循环,第一个循环控制行数,第二个循环控制列数。在第二个循环中,我们使用了字符串格式化来输出乘法表中的每一项,其中 \t 表示制表符,使得每一项之间有一定的间隔。
python九九乘法表矩形_7-2 jmu-python-九九乘法表(矩形) (10 分)
以下是Python实现九九乘法表矩形的代码:
```python
for i in range(1, 10):
for j in range(1, 10):
product = i * j
print('{:2d} x {:2d} = {:2d}'.format(i, j, product), end=' ')
print()
```
输出结果为:
```
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45
6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
```
这段代码使用了两个循环,第一个循环控制乘法表的行数,第二个循环控制乘法表的列数。在循环中使用了字符串格式化,保证每个结果占用两个字符的宽度,便于排列成矩形。同时,使用了`end`参数将每个结果的结尾改为两个空格,便于排列成矩形。在每行的末尾使用了`print()`函数,使得每一行输出后换行。
阅读全文