接收字符串的 Python 函数,返回字符串 + "!"

Hap*_*s31 4 python string return function

正如标题所暗示的那样,我只是想创建一个 Python 函数来接收一个字符串,然后返回该字符串并在末尾添加一个感叹号。

“Hello”的输入应该返回

Hello!
Run Code Online (Sandbox Code Playgroud)

“再见”的输入应该返回

Goodbye!
Run Code Online (Sandbox Code Playgroud)

等等。

这是我尝试过的:

def addExclamation(s):
    s = input("please enter a string")
    new_string = s + "!"
    return new_string

print(s(addExclamation))
Run Code Online (Sandbox Code Playgroud)

这给了我错误信息:

NameError: name 's' is not defined on line 6
Run Code Online (Sandbox Code Playgroud)

为什么没有定义“s”?我以为我确定 s 是addExclamation函数内的输入。感谢您的帮助。

Tig*_*kT3 6

您定义一个带有参数的函数s。该函数立即丢弃该值并要求输入。您正在调用与该参数同名的函数,并向其发送函数名称的参数。那没有任何意义。

def addExclamation(s):
    new_string = s + "!"
    return new_string

print(addExclamation('Hello'))
Run Code Online (Sandbox Code Playgroud)

或者:

def addExclamation():
    s = input("please enter a string")
    new_string = s + "!"
    return new_string

print(addExclamation())
Run Code Online (Sandbox Code Playgroud)