-1

I am a newbie python coder learning for fun. I am wondering why this program has difficulty outputting the correct output. I believe the problem lies with the very end of the program "if list == inverselist:". I always get an output telling me the word is a palindrome even when it is not (e.g. tigers)

#Exercise 6 - Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)
list=[]

for x in possiblepalindrome: #put each individual string character in a new list as its own element
    list.append(x)
print ('this is list', list)

for x in list: #Removes all spaces so multiple word/sentences can be palindrome
    if x is ' ':
        list.remove(' ')
print('this is with removed spaces' , list) 


def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    argument.reverse()
    return argument

inverselist = reverselist(list) #We use the reverselist function to make an inverse list of the palindrome
print('this is inverselist',inverselist)


if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')
  • 1
    You reverse the list in-place and then you compare it to itself. It is always equal to itself. – khelwood Jan 17 '21 at 02:33
  • Note that `input` already produces a string, so `str(input())` is redundant. Also strings can be iterated over and reversed, so building a list out of them is usually unnecessary. `palindrome.replace(' ', '')` will handle eliminating spaces for you. Lastly, for checking if a string is a palindrome, you probably just want to [do this](https://stackoverflow.com/a/17331328/12975140). (Learning to search Stack Overflow / Google is a surprisingly useful coding skill.) – CrazyChucky Jan 17 '21 at 02:36
  • a bit off-topic but important: list is a built-in type in python and you should use other names to name your list variables :) – barshopen Jan 17 '21 at 03:20

4 Answers4

0

list and inverselist have the reference to the same list. You need to clone the list.

Just change the reverselist function

def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    li_copy = [] 
    li_copy.extend(argument)
    li_copy.reverse()
    return li_copy

Entire code:

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)
list=[]

for x in possiblepalindrome: #put each individual string character in a new list as its own element
    list.append(x)
print ('this is list', list)

for x in list: #Removes all spaces so multiple word/sentences can be palindrome
    if x is ' ':
        list.remove(' ')
print('this is with removed spaces' , list) 


def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    li_copy = [] 
    li_copy.extend(argument)
    li_copy.reverse()
    return li_copy

inverselist = reverselist(list) #We use the reverselist function to make an inverse list of the palindrome
print('this is inverselist',inverselist)


if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53
0

You posted far too much code, and didn't bother to test it. reverselist doesn't work the way you expect. It reverses the input list, which is a mutable object.

def reverselist(argument): #This is a function. We put in some list, then the list is reversed, and is spat back out
    argument.reverse()
    return argument

word = list("Hello, world")
print(word)
inverseword = reverselist(word)
print(word, '\n', inverseword)

Output:

['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
['d', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'H'] 
['d', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'H']

In short, you reversed the list, and then compared the result to itself.

Notes:

  • Do not use a pre-defined name as a variable. I changed list to word.
  • Look up how to reverse a sequence. We expect you to do appropriate research before posting here.
  • Learn to test code blocks. See this lovely reference for debugging help.

Your function can be simply:

return argument[::-1]
Prune
  • 76,765
  • 14
  • 60
  • 81
0

First, naming your list list is tempting but dangerous in Python as you will not be able to call the list() constructor

Basically your code could be reduced like this

possiblepalindrome = str(input("Put in the possible palindromic statement here: "))
print(possiblepalindrome)

normal_list = list(possiblepalindrome.remove(" "))
inverselist= list(reversed(possiblepalindrome))
print('this is inverselist',"".join(inverselist))

if list == inverselist:
    print('Congratulations ', '"', possiblepalindrome, '"', ' is a palindrome!')
else:
    print('Unfortunately', '"', possiblepalindrome, '"', 'is not a palindrome.')

0

To check if a string is a palindrome:

v = str(input("Put in the possible palindromic statement here: "))

print (v == (v[::-1]))

Just take the input, compare to the reverse. If those are equal you get a True (so it is a palindrome) otherwise a False.

Synthase
  • 5,849
  • 2
  • 12
  • 34