开始使用免费开始使用

单参数函数

恭喜您!您已经成功地定义并调用了您自己的函数!这很不错。

在上一个练习中,您定义并调用了函数 shout(),它会打印一个与 '!!!' 拼接后的字符串。 现在,您将通过添加一个参数来更新 shout(),使其能够接收并处理传入的任意字符串实参。另请注意,shout(word) 中用于指定函数名和参数的这一部分(函数头的一部分)称为函数的"签名"(signature)。您在其他资料中也可能会遇到这个术语!

本练习是课程的一部分

Python 函数入门

查看课程

练习说明

  • 在函数头中添加参数名 word
  • word'!!!' 拼接后的结果赋值给 shout_word
  • 打印 shout_word 的值。
  • 调用函数 shout(),向其传入字符串 'congratulations'

交互式实操练习

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

# Define shout with the parameter, word
def shout(____):
    """Print a string with three exclamation marks"""
    # Concatenate the strings: shout_word
    ____ = ____ + '!!!'

    # Print shout_word
    print(____)

# Call shout with the string 'congratulations'
编辑并运行代码