There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Whenever I run the code I wrote, it tells me I pass, and I did, but it shows the last print call to be True when it is actually False. Even the print calls I use to compare the two characters show they are DIFFERENT, it still evaluates to True when I compare them.
def is_palindrome(word):
if len(word) < 2:
return True
print(word[0])
print(word[-1])
if word[0] != word[-1]:
return False
return is_palindrome(word[1:-1])
# test cases
print(is_palindrome("abba") == True)
print(is_palindrome("gmcmg") == True)
print(is_palindrome("") == True)
print(is_palindrome("abcd") == False) # Prints True when it is False.
Ah, it’s checking if “abcd” == False if “abcd” == False then this is True. You want all Trues because it means that your code works when it is supposed to and catches the False when it is suppose to.
It’s checking if your function produces a False when “abcd” is passed in. If it does this will print True. If “abcd” produces a True then “abcd” does not == False and it would say False and your code is not doing something it’s supposed to.
Soon as I read your first 7 words, it hit me! I was thinking, for some idiotic reason, that the == False areas of the statements were showing me what it SHOULD be.
Thanks for slapping me in the face and waking me up! I swear, coding for 13 hours makes you get loopy and miss things. Thanks you!
Does the below solution is a good approach recursively? I just wrote it, it did pass all the cases but, I am doubtful… It was not same as the hint given.
def is_palindrome(my_string):
if len(my_string) > 1:
if my_string[0] != my_string[-1]:
return False
else:
return is_palindrome(my_string[1:-1])
return True
I have it done like this. It runs successfully and gives the required result.
My question is, what goes wrong in the code if I do not have return in the elif statement. Why is return a must there?
Because it is necessary in building the call stack. When the string is consumed (whittled down to nothing) the base case is reached and the call stack winds down.
Just throwing this on there after seeing the other discussions for this. Only came here after checking the lesson’s answer against mine and noticing the 4x True result which confused me at first like OP (this bit cleared up here already as the condition for the last one to be True is that the result is False)
The “win condition” of this exercise is funny. I got unreasonably frustrated when I could not for the life of me figure out why the 4th call resulted in an output of “True”. I evaluated by hand, checked on my own machine, debugged using print statements… and still didn’t realize the call was