For a start, instead of declaring an emptyStr â you can just write ''.join(stuff) plus you should never reference anything from outside the function other than constants.
Then for some order - usually you would put the setup first, meaning the definition of the function.
Within your function you reference userInput which is from outside, instead of word which you should use.
As for the check - what are you doing there? First you reverse the word, turn it into a list and turn the list back into a string⌠And then you reverse it again? arenât you reversing the string twice?
The logic behind reversing the reversed string is that we get the original word. Hence I said if original word is equal to reversed reversed string then return âitâs a palindromeâ
You already have the original word that gets passed to the function. All you need to do is compare the original word with the reversed word that you get via reversed.
def checkIfPalindrome(word):
strForPalindrome = emptyStr.join(reversed(word))
print(strForPalindrome)
âââAdding reversed string to list is unnecessary. Just use the emptyStr.join(reversed(word))âââ
if word == strForPalindrome:
print(âitâs a palindromeâ)
else:
print(âitâs not a palindromeâ)
The code worked perfectly as I had envisioned. But one thing I donât get is that strForPalindrome is the reverse of the original word so putting them in the if block shouldnât be equal. As the reversed word is the reverse of the original word.
In the if statement, you are checking if the original word and the reversed word (strForPalindrome) are equal. If they are equal, that means it is a palindrome. If you reverse the string , compare with the original word and they are not equal, then you know it is not a palindrome.