Python 在函数中传递参数+;返回-为什么不返回';这不管用吗?

Python 在函数中传递参数+;返回-为什么不返回';这不管用吗?,python,python-3.x,Python,Python 3.x,我正在使用一个回文检测器,它是使用for循环构建的(这是我参加的课程的一个要求) 我几乎完全完成了它,但是我很难返回一个参数并在最后的函数中使用它。代码如下所示: #-*- coding: utf-8 -*- def main(): intro() text = (input("Inser text here: ")) ordnaText(text) testPalindrom(ordnaText(text)) showResult(testPali

我正在使用一个回文检测器,它是使用for循环构建的(这是我参加的课程的一个要求)

我几乎完全完成了它,但是我很难返回一个参数并在最后的函数中使用它。代码如下所示:

  #-*- coding: utf-8 -*-
def main(): 
    intro()
    text = (input("Inser text here: "))
    ordnaText(text)
    testPalindrom(ordnaText(text))
    showResult(testPalindrom)


def intro():
    print ("Hej! Detta är ett program som testar ifall en text är ett palindrom eller inte.")

def ordnaText (text):
    nytext = ("")
    fixedText = text.lower()
    for i in fixedText:
        if i.isalnum():
            nytext = (nytext + i)
    return nytext

def testPalindrome(nytext):
    palindrome = True
    for i in range (0, len(nytext)):
        if (nytext[i]) != (nytext[len(nytext)-i-1]):
            palindrome = False
    return palindrome

def showResult(palindrome):
    if palindrome == True:
        print ("Yes, this is a palindrome")
    else:
        print ("No, this is not a palindrome.)
main()

除了最后一部分,所有的东西都起作用了:如果我输入“lol”,这是一个回文,它会说这是错误的。“回文”不知何故不能正确返回。我做错了什么?

对于短字符串,要测试回文,只需将其与相反的字符串进行比较:

def testPalindrome(nytext):
    return nytext == nytext[::-1]
您的
testPalindrome
版本工作正常,但是您的方法本身调用
testPalindrom(ordnaText(text))
(末尾没有
e
),所以您可能对该函数有另一个定义

>>> def testPalindrome(nytext):
...     palindrome = True
...     for i in range (0, len(nytext)):
...         if (nytext[i]) != (nytext[len(nytext)-i-1]):
...             palindrome = False
...     return palindrome
... 
>>> testPalindrome('lol')
True
实际上,您并没有将函数的结果传递给
showResult
;而是传递函数。使用变量:

result = testPalindrome(ordnaText(text))
showResult(result)
您可以将其简化一点:

def testPalindrome(nytext):
    for i in range (0, len(nytext)):
        if (nytext[i]) != (nytext[len(nytext)-i-1]):
            return False
    return True
当找到第一个不匹配的字符时,可以提前退出

您不需要在if语句中测试
==True
,只需执行以下操作:

def showResult(palindrome):
    if palindrome:
        print ("Yes, this is a palindrome")
    else:
        print ("No, this is not a palindrome.)
尝试以下方法(代码稍微简洁,逻辑相同):

此外,您没有对返回的结果执行任何操作:

    testPalindrom(ordnaText(text)) #This returns the result, but there is no variable that accepts it


    showResult(testPalindrom)
你可以做:

showResult(testPalindrom(ordnaText(text))) #This will print the returned result
或:


建议:您可以使此代码更短、更整洁。

您的问题主要在于您的主要功能:

ordnaText(text)
这将调用以输入的文本为参数的
ordnaText
函数;然后把结果扔掉

testPalindrom(ordnaText(text))
现在,它使用
ordnaText
方法的结果调用
testPalindrom
函数;然后又一次把结果扔掉。正如您已经调用了
ordnaText
方法一样,最好先存储结果,这样您就可以传递其结果

showResult(testPalindrom)
最后,将函数作为参数调用
showResult
函数。此时,没有任何内容引用输入的文本,您只需传递函数本身。因此,您需要在此处传递
testPalindrom
函数的结果:

text = input("Inser text here: ")
text = ordnaText(text)
testResult = testPalindrom(text)
showResult(testResult)

您必须将testPalindrom的结果传递给showResult

通过做

ordnaText(text)
testPalindrom(ordnaText(text))
showResult(testPalindrom)
将函数本身传递给showResult(它是一个对象),该值不等于True

所以你应该这样做,而不是这三行

showResult(testPalindrom(ordnaText(text)))

这并不能回答我的问题。请回答这个问题,不要回答其他问题。代码的其余部分符合我的喜好。你所有的观点都是有效的观察结果,但都没有回答问题-1我+1因为任务显然是一项家庭作业。那么最好的帮助就是指出问题。函数对象是真的,因为
bool
将其强制为
true
。它不等于
True
,这是代码检查的内容。
ordnaText(text)
testPalindrom(ordnaText(text))
showResult(testPalindrom)
showResult(testPalindrom(ordnaText(text)))