1.先打印8的乘法表:
for looper in [1,2,3,4,5]:
print looper," times 8 = ",looper*8
for looper in range (1,5):
print looper,"times 8 =",looper*8
for looper in range (1,10):
print looper,"times 8 =",looper*8
结果:
>>> ================================ RESTART ================================
>>>
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
5 times 8 = 40
>>>
================================ RESTART ================================
>>>
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
>>> ================================ RESTART ================================
>>>
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
5 times 8 = 40
6 times 8 = 48
7 times 8 = 56
8 times 8 = 64
9 times 8 = 72
>>>
需注意,range(1,5)==[1,2,3,4] 而不等于【1,2,3,4,5】 range()提供一个数字列表,从给定的第一个数开始,在给定的最后一个数之前结束。
2.再打印一般数的乘法表
x=int(raw_input("what multiplication table would you like?\n"))
for i in range (1,11):
print x,"x",i,"=",x*i
print "\n\n***********************************************\n\n"
x=int(raw_input("what multiplication table would you like?\n"))
m=int(raw_input("how high do you want to go ?\n"))
for i in range (1,m+1):
print x,"x",i,"=",x*i
运行结果:
>>> ================================ RESTART ================================
>>>
what multiplication table would you like?
5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
***********************************************
what multiplication table would you like?
7
how high do you want to go ?
12
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
>>>