user9970478
user9970478

Reputation: 3

Trouble with Palindrome function in python

I'm having trouble in an online course for python, specifically a palindrome problem These are the instructions, but the function must be case-insensitive and not see spaces. I think the issue is in my return blocks or my flow. I think I need to use the lower function, but I'm honestly not sure.

def student_func(x):
    for string in x:
        x.lower()
        y = x.replace(" ", "")
        if y[::-1]==y:
            return True
        else:
            return False

Upvotes: 0

Views: 140

Answers (2)

abarnert
abarnert

Reputation: 366113

You actually have two separate problems in your code—and you're right that one of them is with lower and the other is with the return flow.


First, x.lower() doesn't modify x in-place. In fact, strings are immutable; nothing modifies them in-place. If you look up the interactive help or the online docs, it says:

Return a copy of the string with all the cased characters [4] converted to lowercase.

So, you need to do the same thing with lower that you do with replace: assign the result to a variable, and use that:

y = x.lower()
z = y.replace(" ", "")

Or you can reuse the same variable:

x = x.lower()

… or chain the two calls together:

y = x.lower().replace(" ", "")

As a side note, unless you're using Python 2, you should consider whether you want casefold instead of lower. For English it makes no difference, but for other languages it can.


Meanwhile, you're doing for string in x:, but then ignoring string.

If x is just a single word, you don't want to loop over it at all.

If x is a list of words, then the for string in x: is correct, but then you have to use string inside the loop, not x. Plus, you can't just return True or return False—that will exit the function as soon as you test the first word, meaning the rest of them never get tested. I'm not sure whether you want to return True if there are any pallidromes, or if they're all palindromes, or if you want to return a list of booleans instead of a single one, or what, but you can't just return the first one.

It would probably be a lot clearer if you used better names, like words instead of x and word instead of string.

Anyway, I can't tell you the right way to fix this since I don't know what you're trying to do, but hopefully this explains enough that you can fix it yourself.

Upvotes: 3

srj
srj

Reputation: 10151

Giving away the solution defeats the purpose of the exercise

your approach is more or less correct.

  1. convert string to a standard case

  2. remove whitespace

  3. check if reverse of the string is equal to the original string

The error lies in how you are using the python API.

check what each of the functions do, and what they return.

a good idea is to run help(function) to see what the function's documentation has to say about it.

try help(x.lower) (note: not help(x.lower())) and see what the return value is.

Upvotes: 0

Related Questions