-3

I am trying to get my codes to print this output.

here is my code

outputlist = []
bad_character=['! ', ',']

def is_palindrome(alist):

for element in alist:
for s in word:
    if s is bad_character:
        continue
        my_stack.append(s.lower))

        my_stack_reverse=my_stack[::-1]

        if my_stack_reverse==my_stack

        outputlist.append(True)

        else
        append(False)

            if my_stack_reverse=my_stack
   test_list=['Madam', 'A nut for a jar of Tuna', 'I love DSAG']
   print(is_palindrome(test_list))
   print(test_list)

I'm trying to have this as my output but got errors output :

[True, True, False]

['Madam', 'A nut for a jar of Tuna', 'I love DSAG']

  • Does this answer your question? [How to check for palindrome using Python logic](https://stackoverflow.com/questions/17331290/how-to-check-for-palindrome-using-python-logic) – Nick Jan 18 '20 at 04:55
  • Have you made any attempt at debugging this? I recommend reading the following article: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC Jan 18 '20 at 05:20

1 Answers1

0

The code below accomplishes what you want. Make sure that spaces (' ') are included in your bad_character list. I used the replace() method to make the removal of bad characters a bit cleaner, and you can use the lower() method on the whole string all at once, instead of doing it character by character. Also, you can skip the if/else at the end of the function by appending the comparison element == element[::-1] because that will result in the boolean you want.

def ispalindrome(alist):
    outputlist = []
    bad_character = [' ']
    for element in alist:
        for character in bad_character:
            element = element.replace(character, '')
        element = element.lower()
        outputlist.append(element == element[::-1])
    return outputlist

test_list = ['Madam', 'A nut for a jar of Tuna', 'I love DSAG']
print(ispalindrome(test_list))
print(test_list)
schwartz721
  • 767
  • 7
  • 19