num = 1 while num <= 9: tmp = 1 while tmp <= num: print(tmp, "*", num, "=", tmp*num, end=" ") tmp += 1 print("") num += 1
测试结果:
for i in range(1, 10): for j in range(1, i+1): print(str(j) + ' x ' + str(i) + ' = ' + str(i * j) + ' ', end=' ') print('')
for i in range(1, 10): line = '' for j in range(1, i+1): line += '{0}*{1}={2:<2} '.format(j,i,i*j) print(line)
#{2:<2} 对应 i*j, :2 冒号是分割符合,< 表示左对齐,2表示宽度