如何在函数中使用函数作为参数?

时间:2019-07-03 22:59:05

标签: python function parameters

我试图将一个函数用作另一个函数中的参数,但我不断收到错误消息。你们能看看我的代码吗?

#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())

1 个答案:

答案 0 :(得分:1)

我不知道您想要的错误,但是我看到错误:

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

result = remove(sentence)

convertList(result)

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

convertList(remove(sentence))