Day 059 - Project 59 : Palindrome identifier

def palindrome(word):
    cleaned_word = "".join(char for char in word if char.isalnum() or char.isspace())
    cleaned_word_no_spaces = cleaned_word.replace(" ", "")

    if len(cleaned_word_no_spaces) <= 1: 
        return "It's a PalindromešŸŽŠ"
    if cleaned_word_no_spaces[0] != cleaned_word_no_spaces[-1]: 
        return "Booo it's not a palindrome!"
    return palindrome(cleaned_word_no_spaces[1:-1])  

print("Palindrome finder")
user_input = input("Please type a word to find if it's a palindrome or not:\n~ ").strip().lower()
print()
print(palindrome(user_input))
2 Likes

Hi everyone,

First time posting here.
I wrote down this code but I was wondering why this isnā€™t working out:

n[0][number] == j

Thanks for the advice!

word = input("word: ")
l = [word]
n = [word]
amountOfLetters = int(len(word))
count = 0

for i in l:
  for j in i:
    count += 1
    number = int(amountOfLetters - count)
    n[0][number] == j
print(n)

I believe for n[0][number] == j you intend to use a single equal sign.
n[0][number] = j

Remember == compares things to see if they are the same, while = assigns it as something.

a = 1          #single equal sign assigns a as 1 
if a == 1:     #double equal sign checks if a is equal to 1
  print("match")

With this change, you will get an error in the console saying something similar to this but I believe it is a step in the right direction.

Traceback (most recent call last):
  File "/home/runner/yourrepl'sname/main.py", line 11, in <module>
    n[0][number] = j
TypeError: 'str' object does not support item assignment

To illustrate why we are getting this error you can use type() like this

word = input("word: ")
l = [word]
n = [word]
print(type(n))
print(type(n[0]))
#and in your `for  j in i` loop after number = int(amountOfLetters - count)
print(type(n[0][number]))

which outputs :

<class 'list'>
<class 'str'>
<class 'str'>

So while nā€™s type is list, n[0] and n[0][number] are both strings which might be unexpected.

In your code, n[0] is a string, and when you attempt to access n[0][number] , you are accessing a specific character in that string.

In Python, strings are immutable, which means you cannot modify individual characters of a string directly. Once a string is created, you cannot change its idividual characters.

To illustrate this :

word = "hello"
word[0] = "p"
---
word[0] = "p"
TypeError: 'str' object does not support item assignment

On the other hand, lists in Python are mutable, which means you can modify individual elements of a list. You can assign new values to specific positions or indices in a list.

myList = ["a","b","c"]
print(myList)
myList[0] = "z"
print(myList)
---
['a', 'b', 'c']
['z', 'b', 'c']

Using type() again

for i in l:
  for j in i:
    count += 1
    number = int(amountOfLetters - count)
    #n[0][number] = j
    print(type(n))      #<class 'list'>
    print(type(n[0])    #<class 'str'>

So just like in the word = ā€œhelloā€ word[0] = ā€œpā€ example, we yet again can not use item assignment on n[0] because it is a string. Instead we should use item assignment on the list we do have which is n.
Instead of n[0][number] = j use n[number] = j

Now we will get the error

n[number] = j 
IndexError: list assignment index out of range

this is because when you use n = [word] you get ['hello'] so there is only 1 index for n, yet your for loop loops for how many letters there are in word,
So as soon as it gets to the second letter in word there is no n[1] for it to assign j to.
Instead of using n=[word] you should use n = list(word)

word = "hello"
n = [word]
o = list(word)
print(n)
print(o)
---
['hello']
['h', 'e', 'l', 'l', 'o']

This will prevent the IndexError since we do have enough indices for the for loop now

summary of fixes
  1. use n = list(word) instead of n = [word] to prevent the index out of range error.

  2. use n[number] = j instead of n[0][number] == j to avoid the str object does not support item assignment TypeError:


If you need more help see these, (with comments or without comments) where I go through the changes I made to get your code to work.

P.S. my apologies for the many edits.

3 Likes

Great advice thanks!

I fixed the code and it works just fine.

word = input("word: ")
l = list(word)
n = list(word)
amountOfLetters = int(len(word))
count = 0
reversedWord = ""

for i in l:
  count += 1
  number = int(amountOfLetters - count)
  n[number] = i

reversedWord = "".join(n)
print(f"reversed: {reversedWord}")

if word == reversedWord:
  print("palindrome")

else:
  print("no palindorme")
1 Like