python中打印九九乘法表

如题所述

1、使用for循环打印九九乘法表:
```python
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
```
2、使用while循环打印九九乘法表:
```python
i = 1
while i <= 9:
j = 1
while j <= i:
print('%d*%d=%-3d' %(i, j, i*j), end='\t' )
j += 1
print('')
i += 1
```
3、结合while和for循环打印九九乘法表:
```python
i = 1
while i <= 9:
for j in range(1, i+1): # range()函数左闭右开
print('%d*%d=%-3d'%(i,j,i*j),end='')
i += 1
print()
```
4、使用for循环和while循环打印九九乘法表:
```python
for i in range(1, 10):
j = 0
while j < i:
j += 1
print("%d*%d=%-3d"%(i,j,i*j),end='')
print()
```
5、定义一个变量a并使用循环打印九九乘法表:
```python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
j = 1
while j <= i:
print('%d*%d=%-3d'%(i,j,i*j),end='\t')
j += 1
print()
```
6、使用一行语句打印九九乘法表:
```python
print('\n'.join([' '.join(["%2s x%2s = %2s" % (j, i, i*j) for j in range(1, i+1)]) for i in range(1, 10)]))
```
温馨提示:答案为网友推荐,仅供参考
相似回答