Please tell me your name:
First_name: li
Last_name: yege
Whether tocontinue running (y/n): y
Please tell me your name:
First_name: wang
Last_name: erxiao
Whether tocontinue running (y/n): n
defgreet_users(names):
'''向列表中的每位用户都发出简单的问候'''for name in names:
msg = "Hello , " + name.title() + "!"print(msg)
usernames = ['hahaha','try','margot']
greet_users(usernames)
Hello , Hahaha!
Hello , Try!
Hello , Margot!
函数中修改列表
将列表传递给函数后,函数就可以对其进行修改
函数中对列表所作的任何修改都是永久性的,可以高效地处理大量数据
# 不使用函数传递# 首先创建一个列表,其中包含一些要打印的设计
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
# 模拟打印每个设计,直到没有未打印的设计为止# 打印每个设计后,都将其移到列表 completed_models 中while unprinted_designs:
# 模拟根据设计制作 3D 打印模型的过程
current_design = unprinted_designs.pop()
print("Printing model: " + current_design)
completed_models.append(current_design)
# 显示打印好的所有模型print("\nThe following models have been printed: ")
for completed_model in completed_models:
print(completed_model)
Printing model:dodecahedronPrinting model:robotpendantPrinting model:iphonecaseThe following models have been printed:dodecahedronrobotpendantiphonecase
# 使用函数传递列表defprint_models(unprinted_designs,completed_models):
while unprinted_designs:
# 模拟根据设计制作 3D 打印模型的过程
current_design = unprinted_designs.pop()
print("Printing model: " + current_design)
completed_models.append(current_design)
defshow_completed_models(completed_models):
print("\nThe following models have been printed: ")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)
print(unprinted_designs)
Printing model:dodecahedronPrinting model:robotpendantPrinting model:iphonecaseThe following models have been printed:dodecahedronrobotpendantiphonecase
[]
# 使用函数传递列表,禁止函数修改列表,可以使用里列表的副本,使用切片defprint_models(unprinted_designs,completed_models):
while unprinted_designs:
# 模拟根据设计制作 3D 打印模型的过程
current_design = unprinted_designs.pop()
print("Printing model: " + current_design)
completed_models.append(current_design)
defshow_completed_models(completed_models):
print("\nThe following models have been printed: ")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
print_models(unprinted_designs[:],completed_models)
show_completed_models(completed_models)
print(unprinted_designs)
Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone case
The following models have been printed:
dodecahedron
robot pendant
iphone case
['iphonecase', 'robot pendant', 'dodecahedron']
练习
# 1.魔术师# 创建一个包含魔术师名字的列表,并将其传递给一个名为 show_magicians() 的函数,这个函数打印列表中每个魔术师的名字
names = ['liuqian','xiaowang','zhangsan','lisi']
defshow_magicians(names):
for name in names:
print("The magicians names is " + name.title() + ".")
show_magicians(names)
The magicians names is Liuqian.
The magicians names is Xiaowang.
The magicians names is Zhangsan.
The magicians names is Lisi.
# 2.了不起的魔术师# 在你为完成练习编写的程序中,编写一个名为 make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样 “the# Great” 。调用函数 show_magicians() ,确认魔术师列表确实变了
names = ['liuqian','xiaowang','zhangsan','lisi']
defshow_magicians(names):
for name in names:
print("The magicians names is " + name.title() + ".")
defmake_great(names):
great_magicians = []
while names:
new_names = names.pop()
great_magician = new_names + "the Great"
great_magicians.append(great_magician)
for great_magician in great_magicians:
names.append(great_magician)
show_magicians(names)
print("\n")
make_great(names)
show_magicians(names)
The magicians names is Liuqian.
The magicians names is Xiaowang.
The magicians names is Zhangsan.
The magicians names is Lisi.
The magicians names is Lisithe Great.
The magicians names is Zhangsanthe Great.
The magicians names is Xiaowangthe Great.
The magicians names is Liuqianthe Great.
# 3.不变的魔术师# 在调用函数 make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的# 列表,并将其存储到另一个列表中。分别使用这两个列表来调用 show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字# 样 “the Great” 的魔术师名字。defshow_magicians(names):
for name in names:
print("The magicians names is " + name.title() + ".")
defmake_great(names):
great_magicians = []
while names:
new_names = names.pop()
great_magician = new_names + "the Great"
great_magicians.append(great_magician)
print(names)
for great_magician in great_magicians:
names.append(great_magician)
print(names)
names = ['liuqian','xiaowang','zhangsan','lisi']
show_magicians(names)
print("\n")
copy = names[:]
make_great(copy)
show_magicians((copy))
The magicians names is Liuqian.
The magicians names is Xiaowang.
The magicians names is Zhangsan.
The magicians names is Lisi.
['liuqian', 'xiaowang', 'zhangsan']['liuqian', 'xiaowang']['liuqian'][]['lisithe Great']['lisithe Great', 'zhangsanthe Great']['lisithe Great', 'zhangsanthe Great', 'xiaowangthe Great']['lisithe Great', 'zhangsanthe Great', 'xiaowangthe Great', 'liuqianthe Great']
The magicians names is Lisithe Great.
The magicians names is Zhangsanthe Great.
The magicians names is Xiaowangthe Great.
The magicians names is Liuqianthe Great.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构