如标题所示,我只是想创建一个 Python 函数来接收一个字符串,然后返回该字符串并在末尾添加一个感叹号。
“你好”的输入应该返回
Hello!
“再见”的输入应该返回
Goodbye!
等等。
这是我尝试过的:
def addExclamation(s):
s = input("please enter a string")
new_string = s + "!"
return new_string
print(s(addExclamation))
这给了我错误信息:
NameError: name 's' is not defined on line 6
为什么没有定义“s”?我以为我确定 s 是 addExclamation
函数中的输入。感谢您的帮助。
最佳答案
您使用参数 s
定义了一个函数。该函数立即丢弃该值并请求输入。您正在调用一个与该参数同名的函数,并向它发送一个函数名称的参数。这没有任何意义。
def addExclamation(s):
new_string = s + "!"
return new_string
print(addExclamation('Hello'))
或者:
def addExclamation():
s = input("please enter a string")
new_string = s + "!"
return new_string
print(addExclamation())
关于接收字符串的 Python 函数,返回字符串 + "!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41233106/