0

I am working with a Palindrome-detector that is built using for-loops (this was a requirement for the course I am attending).

I am almost fully done with it but I am having trouble returning an argument and using it in the final function. The code looks like this:

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

Everything works except the final part: if I put in "lol" which is a palindrome it says that it's false. "palindrome" somehow doesn't return correctly. What am I doing wrong?

4 Answers 4

4

For short strings, to test for palindromes just compare it against the reverse:

def testPalindrome(nytext):
    return nytext == nytext[::-1]

Your version of testPalindrome works fine, but your method itself calls testPalindrom(ordnaText(text)) (no e at the end), so perhaps you have another definition of the function?

>>> 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

You don't actually pass the result of the function to showResult; you pass the function instead. Use a variable:

result = testPalindrome(ordnaText(text))
showResult(result)

You can simplify this down a little:

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

You can exit early when you find the first non-matching character.

You don't need to test for == True in if statements, just do:

def showResult(palindrome):
    if palindrome:
        print ("Yes, this is a palindrome")
    else:
        print ("No, this is not a palindrome.)
Sign up to request clarification or add additional context in comments.

4 Comments

This does not answer my question.
Please just answer the question and nothing else. The rest of the code works to my liking.
All your points are valid observations, but none answer the question. -1
My +1 as the task is apparently a homework. Then the best help is to point to the issues.
3

Your problem mainly lies in your main function:

ordnaText(text)

This calls the ordnaText function with the entered text as its parameter; and then it throws the result away.

testPalindrom(ordnaText(text))

Now this calls the testPalindrom function with the result from the ordnaText method; and then again it throws the result away. As you have already called the ordnaText method, it is also a good idea to store the result before, so you can just pass its result.

showResult(testPalindrom)

And finally, you call the showResult function with the function as a parameter. At this point, there is nothing that refers to the entered text, you just pass the function itself. So what you want to do here is pass the result of the testPalindrom function here:

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

Comments

1

Try this (slightly cleaner code, same logic) :

def testPalindrome(str) :
    for i in range(int(len(str)/2)) :
        if str[i] != str[len(str)-i-1] :
             return False

    return True

Also, you arent doing anything with the result returned :

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


    showResult(testPalindrom)

You could do :

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

Or :

    result = testPalindrom(ordnaText(text))

    showResult(result)

Advice : You can make this code much much shorter and tidier.

Comments

0

You have to pass the result of testPalindrom to showResult

By doing

ordnaText(text)
testPalindrom(ordnaText(text))
showResult(testPalindrom)

you pass the function itself to showResult (which is an object) which is not equal to True

so you should do instead of this three lines

showResult(testPalindrom(ordnaText(text)))

1 Comment

A function object is true in that bool coerces it into True. It is not equal to True, which is what the code checks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.