上一章的小游戏,玩的开心不,接下来,我们要讨论些其他方面的知识点
先说说循环,还是循环,形成所谓的嵌套循环
很简单的乘法表代码
multiplier = 5
for i in range (1, 11):
print i, "x", multiplier, "=", i * multiplier
IDLE运行结果
>>>
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
>>>
这是一个数的乘法表,如果我们要三个数的乘法表呢,这就是嵌套循环最擅长的
for multiplier in range (5, 8):
for i in range (1, 11):
print i, "x", multiplier, "=", i * multiplier
print
运用结果
>>>
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
6 x 7 = 42
7 x 7 = 49
8 x 7 = 56
9 x 7 = 63
10 x 7 = 70
>>>
上面写得在实际中都不实用的,为什么,可变循环可以由用户来控制
numStars = int(raw_input ("how many stars do you want? "))
for i in range (1, numStars):
print '*',
运行结果
>>>
how many stars do you want? 6
* * * * *
>>>
发现没有,希望得到6个星星,结果输出了5个。为什么呢,想想。
numStars = int(raw_input ("how many stars do you want? "))
for i in range (1, numStars + 1):
print '*',
这次结果
>>>
how many stars do you want? 6
* * * * * *
>>>
还有一种方法,for循环从0开始计数,也是可以的。
numStars = int(raw_input ("how many stars do you want? "))
for i in range (0, numStars):
print '*',
那可变嵌套循环呢
numLines = int(raw_input ('how many lines of stars do you want? '))
numStars = int(raw_input ("how many stars per line? "))
for line in range (0, numLines):
for star in range(0, numStars):
print '*',
print
运行结果
>>>
how many lines of stars do you want? 6
how many stars per line? 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
>>>
这样有点意思了吧,甚至,还有嵌套嵌套循环
numBlocks = int(raw_input ('how many blocks of stars do you want? '))
numLines = int(raw_input ('how many lines of stars do you want? '))
numStars = int(raw_input ("how many stars per line? "))
for block in range(0, numBlocks):
for line in range (0, numLines):
for star in range(0, numStars):
print '*',
print
print
运行结果如下
>>>
how many blocks of stars do you want? 3
how many lines of stars do you want? 5
how many stars per line? 4
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
>>>
这也是双重嵌套循环,更多可变嵌套循环,还可以写个更复杂的可变嵌套循环
numBlocks = int(raw_input ('how many blocks of stars do you want? '))
for block in range(1, numBlocks + 1):
for line in range(1, block * 2 ):
for star in range(1, (block + line) * 2):
print '*',
print
print
运行结果
>>>
how many blocks of stars do you want? 3
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
>>>
还可以直接打印出循环变量的值
numBlocks = int(raw_input ('how many blocks of stars do you want? '))
for block in range(1, numBlocks + 1):
print 'block = ', block
for line in range(1, block * 2 ):
for star in range(1, (block + line) * 2):
print '*',
print ' line =', line, 'star = ', star
print
运行结果
>>>
how many blocks of stars do you want? 3
block = 1
* * * line = 1 star = 3
block = 2
* * * * * line = 1 star = 5
* * * * * * * line = 2 star = 7
* * * * * * * * * line = 3 star = 9
block = 3
* * * * * * * line = 1 star = 7
* * * * * * * * * line = 2 star = 9
* * * * * * * * * * * line = 3 star = 11
* * * * * * * * * * * * * line = 4 star = 13
* * * * * * * * * * * * * * * line = 5 star = 15
>>>
那使用嵌套循环能做些什么实际工作呢,他最擅长的其实就是得出一系列决定的所有可能的排列和组合。下面举个例子,热狗店里做一个广告海报,用数字显示如何订购热狗、小面包、番茄、芥末酱和洋葱的所有可能的组合。我们需要得出总共有多少种可能的组合。需要用到一种方法就是使用决策树(decision tree),用代码实现
print "\tDog \tBun \tKetchup\tMustard\tOnions"
count = 1
for dog in [0, 1]:
for bun in [0, 1]:
for ketchup in [0, 1]:
for mustard in [0, 1]:
for onion in [0, 1]:
print "#", count, "\t",
print dog, "\t", bun, "\t", ketchup, "\t",
print mustard, "\t", onion
count = count + 1
运行结果
>>>
Dog Bun Ketchup Mustard Onions
# 1 0 0 0 0 0
# 2 0 0 0 0 1
# 3 0 0 0 1 0
# 4 0 0 0 1 1
# 5 0 0 1 0 0
# 6 0 0 1 0 1
# 7 0 0 1 1 0
# 8 0 0 1 1 1
# 9 0 1 0 0 0
# 10 0 1 0 0 1
# 11 0 1 0 1 0
# 12 0 1 0 1 1
# 13 0 1 1 0 0
# 14 0 1 1 0 1
# 15 0 1 1 1 0
# 16 0 1 1 1 1
# 17 1 0 0 0 0
# 18 1 0 0 0 1
# 19 1 0 0 1 0
# 20 1 0 0 1 1
# 21 1 0 1 0 0
# 22 1 0 1 0 1
# 23 1 0 1 1 0
# 24 1 0 1 1 1
# 25 1 1 0 0 0
# 26 1 1 0 0 1
# 27 1 1 0 1 0
# 28 1 1 0 1 1
# 29 1 1 1 0 0
# 30 1 1 1 0 1
# 31 1 1 1 1 0
# 32 1 1 1 1 1
>>>
还可以把每样的卡路里值也跟在后面,
dog_cal = 140
bun_cal = 120
ket_cal = 80
mus_cal = 20
onion_cal = 40
print "\tDog \tBun \tKetchup\tMustard\tOnions\tCalories"
count = 1
for dog in [0, 1]:
for bun in [0, 1]:
for ketchup in [0, 1]:
for mustard in [0, 1]:
for onion in [0, 1]:
total_cal = (bun * bun_cal)+(dog * dog_cal) + \
(ketchup * ket_cal)+(mustard * mus_cal) + \
(onion * onion_cal)
print "#", count, "\t",
print dog, "\t", bun, "\t", ketchup, "\t",
print mustard, "\t", onion,
print "\t", total_cal
count = count + 1
运行结果
>>>
Dog Bun Ketchup Mustard Onions Calories
# 1 0 0 0 0 0 0
# 2 0 0 0 0 1 40
# 3 0 0 0 1 0 20
# 4 0 0 0 1 1 60
# 5 0 0 1 0 0 80
# 6 0 0 1 0 1 120
# 7 0 0 1 1 0 100
# 8 0 0 1 1 1 140
# 9 0 1 0 0 0 120
# 10 0 1 0 0 1 160
# 11 0 1 0 1 0 140
# 12 0 1 0 1 1 180
# 13 0 1 1 0 0 200
# 14 0 1 1 0 1 240
# 15 0 1 1 1 0 220
# 16 0 1 1 1 1 260
# 17 1 0 0 0 0 140
# 18 1 0 0 0 1 180
# 19 1 0 0 1 0 160
# 20 1 0 0 1 1 200
# 21 1 0 1 0 0 220
# 22 1 0 1 0 1 260
# 23 1 0 1 1 0 240
# 24 1 0 1 1 1 280
# 25 1 1 0 0 0 260
# 26 1 1 0 0 1 300
# 27 1 1 0 1 0 280
# 28 1 1 0 1 1 320
# 29 1 1 1 0 0 340
# 30 1 1 1 0 1 380
# 31 1 1 1 1 0 360
# 32 1 1 1 1 1 400
>>>
里面用到了一些现在我们还没接触到的知识,没关系,接下来会一一来了解到的,很简单。我们用了制表符来对齐,也就是符号“\t”,还有个符号要注意,代码行末的反斜线“\”,表示这一行还没结束,下一行也是这一行的内容。
怎么样,这么枯燥的内容,编写一个程序来实现,是不是很有趣啊。