如何将函数用作函数中的参数?

2024-06-16 13:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在另一个函数中使用一个函数作为参数,但我一直得到一个错误。你们能看看我的密码吗

#Program used to reverse the order of a string.

sentence = input("Write a sentence. After you click enter it will be returned in reverse order. ")
userInputList = [ ]
reverseInputList = [ ]

#remove any periods from the sentence.
def remove(sentence):
  stripped = sentence.replace(".", "")
  return stripped

#print string in reverse order
def convertList(stripped):
  userInputList = stripped.split()
  reverseInputList = userInputList[::-1]

  for i in range(len(reverseInputList)):
   print (reverseInputList[i], end=" ")


remove(sentence)

convertList(remove())

Tags: the函数in参数stringdefordersentence
1条回答
网友
1楼 · 发布于 2024-06-16 13:06:15

我不知道你会犯什么错误,但我看到了错误:

您必须将来自remove(sentence)的结果分配给变量,并在convertList()中使用此结果

result = remove(sentence)

convertList(result)

或者您必须在convertList()中直接使用带有参数remove(sentence)的函数

convertList(remove(sentence))

相关问题 更多 >