Python面试题(二)

时间:2021-05-19 20:38:17

打印九九乘法表

思路:利用字符串的连接,梯形输出结果

>>> def st(num):
... l = []
... for x in xrange(1, num + 1):
... str1 = ''
... for y in xrange(1, x + 1):
... str1 = str1 + '%d * %d = %s ' % (y, x, str(y*x))
... l.append(str1)
... for i in l:
... print i
...
>>>
>>>
>>> st(3)
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9

generated by haroopad