题目:打印出完整的99乘法表。
代码:
#!/usr/bin/python3
for x in range(1,10):
for y in range(1,x+1):
r=x*y
print ('%d x %d = %-2d' %(y,x,r),' ',end='')#%d格式化成整数,- 代表左对齐,数字代表占位。
print (end='\n')#这一句代表,每次遍历完一个周期换行,并下一次遍历的结果将从该行输出。如果是print()将从下一行开始输出。
结果:
>>>
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49