开始使用免费开始使用

返回单个值的函数

您做得非常好!请再次修改 shout() 函数,使其现在在函数内部不打印,而是返回单个值。回顾一下,return 关键字可让您从函数中返回值。这里展示了您之前编写的 shout() 函数的部分代码。通常返回值比直接打印更可取,因为正如之前所见,将 print() 调用赋给变量时,其类型会是 NoneType

本练习是课程的一部分

Python 函数入门

查看课程

练习说明

  • 在函数体内,将 word 中的字符串与 '!!!' 连接,并赋值给 shout_word
  • print() 语句替换为合适的 return 语句。
  • 调用 shout() 函数,传入字符串 'congratulations',并将该调用赋值给变量 yell
  • 为检查 yell 是否包含 shout() 返回的值,打印 yell 的值。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Define shout with the parameter, word
def shout(word):
    """Return a string with three exclamation marks"""
    # Concatenate the strings: shout_word
    

    # Replace print with return
    print(shout_word)

# Pass 'congratulations' to shout: yell


# Print yell
编辑并运行代码