传递列表
def greet_users(names):
"""像列表中的每位用户发出问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['js','jzh','jjf']
greet_users(usernames)
image.png
在函数中修改列表
def print_models(unprinted_designs,completed_models):
"""
打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到列表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
#模拟根据设计制作3D打印的模型的过程
print("Printing model: " + current_design)
completed_models.append(current_design)
def show_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)
image.png
魔术师
def show_magicians(names):
"""打印每位魔术师的名字"""
for name in names:
print(name)
m_name = ['js','jzh','jjf']
show_magicians(m_name)
image.png
def show_magicians(names):
"""打印每位魔术师的名字"""
for name in names:
print(name)
def make_great(names):
great_magicians = []
while names:
name = names.pop()
great_magician = name + " the Great"
great_magicians.append(great_magician)
for great_magician in great_magicians:
print(great_magician)
names = ['js','jzh','jjf']
show_magicians(m_name)
make_great(names)
show_magicians(names)
image.png
网友评论