python课的小程序~ 乘法口诀表

这应该算是真正意义上的第一个python程序了吧,还是完成作业
作业就是打印个乘法口诀表,真心比较难搞(以后再看这句话估计要笑了)

Question

# 2013.11.01 by Alex YU
# Three parts: assignment b[] & a[] & print
# Notice: the len should be fixed. by using .ljust(6) ~~~~

# assignment b[]~
b = []
i = 0
j = 0
r = 1
s = 0
while j < 9:
     i = 0
     s = 0
     while i < 10 - r:
          b.append(str(r) + 'x' + str(r+s) + '=' + str(r*(r+s)))
          s = s + 1
          i = i + 1
     r = r + 1
     j = j + 1

a = [['' for x in range(9)]for y in range(9)]
i = 0
j = 0
k = 0
while j < 9:
     i = j
     while i < 9:
          a[i][j] = b[k]
          k = k + 1
          i = i + 1
     j = j + 1

for i in a:
     for j in i:
          print  j.ljust(6),
     print
#

已经笑了!,研究了好久之后终于诞生了Beta Ver.


# 2013.11.13 by Alex YU
# Beta Ver.

for i in range(1,10):
    for j in range(1, i+1):
        print "%dx%d=%-2d"%(j,i,i*j),# "%-2d"!!!
    print
#

又笑了!


# 2013.11.13 by Alex YU
# beyond

print ("\n".join("".join("%dx%d=%-3d" % (j,i,i*j) for j in  range(1,i+1))for i in range(1,10)))
#

发表评论

电子邮件地址不会被公开。 必填项已用*标注