第一种方法(for循环)
# 九九乘法表
for i in range(1, 10):
for j in range(1, i+1):
print(f'{j}x{i}={i*j}\t', end='')
print()
第二种方法(while)
# 九九乘法表
i = 1
while i <= 9:
j = 1
while(j <= i):
print(f'{i}*{j}={i*j}', end='\t')
j += 1
print('')
i += 1
第三种方法(while+for)
i = 1
while i <= 9:
for j in range(1, i+1):
print(f'{i}*{j}={i*j}', end=' ')
i += 1
print()
第四种方法(for+while)
for i in range(1, 10):
j = 0
while j < i:
j += 1
print(f"{i}*{j}={i*j}", end=' ')
print()
第五种方式(写列表赋值变量)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
j = 1
while j <= i:
print(f'{i}*{j}={i*j}', end='\t')
j += 1
print()
第六种方式(递归方式)
def multiplication(n):
if n < 10:
for m in range(1, n+1):
print(f"{m}*{n}={m*n}", end="\t")
print()
multiplication(n+1)
multiplication(1)
第七种
print('\n'.join([' '.join([f"{j}x{i}={i*j}" for j in range(1, i + 1)]) for i in range(1, 10)]))
© 版权声明
THE END
请登录后查看评论内容