Python入门习题大全——不变的魔术师

Python入门习题大全——索引

修改你为完成练习“了不起的魔术师”而编写的程序,在调用函数make_great()时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_ magicians(),确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“theGreat”的魔术师名字。

# 不变的的魔术师
def make_great(names, names2):
    while names:
        name = "the great " + names.pop()
        names2.append(name)
def show_magicians(names):
    for name in names:
        print(name)

names = ['ling', 'hui', 'he']
names2 = []
make_great(names[:], names2)
show_magicians(names2)
show_magicians(names)

输出为:

the great he
the great hui
the great ling
ling
hui
he
发布了269 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43479432/article/details/105425603