Python Forum
Palindrome in Python - result always false
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Palindrome in Python - result always false
#4
it works for me when indentation is correct

def is_palindrome(word):
    word_start = ''
    for letter in word:
        word_start = letter + word_start
        if word == word_start:
            return True
 
    return False

for word in ['radar', 'blabla']:
    print(f"{word} ==> {is_palindrome(word)}")
Output:
radar ==> True blabla ==> False
and for improvements, well
def is_palindrome(word):
    return word == word[::-1]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Palindrome in Python - result always false - by buran - Oct-16-2019, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Palindrome Inkanus 4 2,173 Nov-25-2020, 04:11 PM
Last Post: perfringo
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,283 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Post JSON from python to PHP don't give expected result pgagnebin 1 3,809 Sep-04-2019, 10:29 PM
Last Post: micseydel
  python hmac gave different result than php hash_hmac nadavvin 2 3,232 Feb-18-2019, 03:35 PM
Last Post: nadavvin
  Palindrome mp3909 3 4,273 Oct-18-2017, 01:54 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020