Python编程入门到实践课后习题8-9,8-10,8-11
8-9 魔术师: 创建一个包含魔术师名字的列表, 并将其传递给一个名为show_magicians()的函数
def show_magicians(names):
for name in names:
print(name)
8-10 了不起的魔术师: 在上题的基础上,编写一个名为make_great()的函数,对魔术师列表进行修改,在魔术师的名字中都加入字样"the Great"。调用函数show_magicians(),确认魔术师列表确实变了。
def show_magicians(names):
for name in names:
print(name)
def make_great(names):
i = 0
while True:
if i >= len(names):
break
names[i] = "the Great " + names[i]
i = i + 1
names = ['yanjiahui', 'liuqian', 'zhoujielun']
show_magicians(names)
make_great(names)
show_magicians(names)
8-11 不变的魔术师:在上题的基础上,在调用函数make_great()时,向他传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians(),确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样的"the Great"的魔术师名字
def show_magicians(names):
for name in names:
print(name)
def make_great(names, addnames):
while names:
name = names.pop()
name = "the Great " + name
addnames.append(name)
names = ['yanjiahui', 'liuqian', 'zhoujielun']
addnames = []
show_magicians(names)
make_great(names[:], addnames)
show_magicians(addnames)