Python 速成班练习 8-11:请说明 return 语句的工作原理
Python crash course Exercise 8-11: Please clarify how the return statement works
我正在阅读“Python 速成课程”这本书,我已经完成了一个关于将列表的副本作为参数传递给函数的练习。我解决不了问题,作者提供的解决方案我也不是很清楚。
这是练习文本:
“不变的魔术师:从练习 8-10 开始。使用魔术师姓名列表的副本调用函数 make_great() 。 Because the original list will be unchanged, return the new list and store it in a separate list
。对每个列表调用 show_magicians() 以显示您有一个原始名称列表和一个列表,其中每个魔术师的名字都添加了 Great。“
我的问题确实出在突出显示的行上。为什么需要 return 语句?
我的意思是一旦执行下面代码中的 return magicians 语句,实际上会发生什么?
能否请您也向我解释一下我在注释掉 return 语句时遇到的错误:TypeError: 'NoneType' object is not iterable?
def show_magicians(magicians):
"""Print the name of each magician in the list."""
for magician in magicians:
print(magician)
def make_great(magicians):
"""Add 'the Great!' to each magician's name."""
# Build a new list to hold the great musicians.
great_magicians = []
# Make each magician great, and add it to great_magicians.
while magicians:
magician = magicians.pop()
great_magician = magician + ' the Great'
great_magicians.append(great_magician)
# Add the great magicians back into magicians.
for great_magician in great_magicians:
magicians.append(great_magician)
return magicians
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)
print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)
print("\nOriginal magicians:")
show_magicians(magicians)
在此先感谢您!
Return 语句是必需的,因为您要存储 make_great
的结果,即魔术师列表。
great_magicians = make_great(magicians[:])
如果您不提供 return 声明,None
将被 return 编辑。所以你不能在 show_magicians
.
中遍历 None
# without return statement great_magicians will be None
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)
首先让我解释一下return语句是如何工作的,函数会继续执行直到有return语句。 return 语句用于结束函数的执行,它 return 将值传递给它的调用者,return 语句之后的代码不会被执行。
例如考虑以下代码。
def get_full_name(first_name, last_name):
"""the function takes first_name and last_name as parameter the concatenates both values and returns the full name"""
return first_name + " " + last_name
# if there's further code after the return statement it won't execute.
full_name = get_full_name("Ali", "Muhammad")
# the value returned by get_full_name function will be assigned to full_name variable.
顺便说一句,您的代码看起来不错!可能在
处有缩进
for great_magician in great_magicians:
magicians.append(great_magician)
return magicians
您可以在此处查看工作代码:https://colab.research.google.com/drive/1Ie9Nd9jlgKW5JTx_ZQdAkkEwykarDfv_?usp=sharing
我正在阅读“Python 速成课程”这本书,我已经完成了一个关于将列表的副本作为参数传递给函数的练习。我解决不了问题,作者提供的解决方案我也不是很清楚。
这是练习文本:
“不变的魔术师:从练习 8-10 开始。使用魔术师姓名列表的副本调用函数 make_great() 。 Because the original list will be unchanged, return the new list and store it in a separate list
。对每个列表调用 show_magicians() 以显示您有一个原始名称列表和一个列表,其中每个魔术师的名字都添加了 Great。“
我的问题确实出在突出显示的行上。为什么需要 return 语句?
我的意思是一旦执行下面代码中的 return magicians 语句,实际上会发生什么?
能否请您也向我解释一下我在注释掉 return 语句时遇到的错误:TypeError: 'NoneType' object is not iterable?
def show_magicians(magicians):
"""Print the name of each magician in the list."""
for magician in magicians:
print(magician)
def make_great(magicians):
"""Add 'the Great!' to each magician's name."""
# Build a new list to hold the great musicians.
great_magicians = []
# Make each magician great, and add it to great_magicians.
while magicians:
magician = magicians.pop()
great_magician = magician + ' the Great'
great_magicians.append(great_magician)
# Add the great magicians back into magicians.
for great_magician in great_magicians:
magicians.append(great_magician)
return magicians
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)
print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)
print("\nOriginal magicians:")
show_magicians(magicians)
在此先感谢您!
Return 语句是必需的,因为您要存储 make_great
的结果,即魔术师列表。
great_magicians = make_great(magicians[:])
如果您不提供 return 声明,None
将被 return 编辑。所以你不能在 show_magicians
.
None
# without return statement great_magicians will be None
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)
首先让我解释一下return语句是如何工作的,函数会继续执行直到有return语句。 return 语句用于结束函数的执行,它 return 将值传递给它的调用者,return 语句之后的代码不会被执行。
例如考虑以下代码。
def get_full_name(first_name, last_name):
"""the function takes first_name and last_name as parameter the concatenates both values and returns the full name"""
return first_name + " " + last_name
# if there's further code after the return statement it won't execute.
full_name = get_full_name("Ali", "Muhammad")
# the value returned by get_full_name function will be assigned to full_name variable.
顺便说一句,您的代码看起来不错!可能在
处有缩进for great_magician in great_magicians:
magicians.append(great_magician)
return magicians
您可以在此处查看工作代码:https://colab.research.google.com/drive/1Ie9Nd9jlgKW5JTx_ZQdAkkEwykarDfv_?usp=sharing