学习python循环语句,四种方法实现做九九乘法表。
def jiujiuWhile(a): ‘‘‘use WHILE for loop‘‘‘ i=1 while i<=a: j=1 while j<=i: print "%d X %d = %d\t" % (j,i,i*j), j+=1 i+=1 print
def jiujiuFor(a): ‘‘‘use FOR for loop‘‘‘ for i in range(1, a+1) : for j in range(1, i+1) : print "%d x %d = %d\t" % (j,i,j*i), print
def ThisLine(a): print "1 X %d = %d" % (a,a), for i in range(2,a+1): print "\t%d X %d = %d" % (i,a,i*a), print
def jiujiuDigui(m): if m>1: jiujiuDigui(m-1) ThisLine(m) elif m==1: ThisLine(1)
def jiujiuList(a): ‘‘‘如何能用map()函数一次计算出这个二维小九九数组?(这样可以不用for了) 如何能够以类似前面函数那样更规范的格式显示出来?‘‘‘ for i in range(1,a+1): print map(lambda x: "%d X %d = %d" %(x,i,x*i) ,range(1,i+1))
jiujiuFor(9) jiujiuWhile(9) jiujiuDigui(9) jiujiuList(9)
本文出自 “IT改变我们” 博客,请务必保留此出处http://charlywoo.blog.51cto.com/4013957/1542979
原文地址:http://charlywoo.blog.51cto.com/4013957/1542979