我们在学习Python的过程中需要不断的积累和练习,这样才能够走的更远,今天一起来学习怎么用Python写九九乘法表~
第一种方法、for-for
代码:
for i in range(1, 10):
for j in range(1, i+1):
print(f'{j}x{i}={i*j}\t', end='')
print()
i = 1
while i <= 9:
j = 1
while(j <= i): # j的大小是由i来控制的
print(f'{i}*{j}={i*j}', end='\t')
j += 1
print('')
i += 1
i = 1
while i <= 9:
for j in range(1, i+1): # range()函数左闭右开
print(f'{i}*{j}={i*j}', end=' ')
i += 1
print()
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')
# %-3d 是控制输出结果占据3位,且从左面开始对齐
j += 1
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')
# %-3d 是控制输出结果占据3位,且从左面开始对齐
j += 1
print()
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(["%2s x%2s = %2s" % (j, i, i*j) for j in range(1, i+1)]) for i in range(1, 10)]))
print('\n'.join([' '.join([f"{j}x{i}={i*j}" for j in range(1, i + 1)]) for i in range(1, 10)]))