Python编程入门到实践课后习题8-9,8-10,8-11

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)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值