Search code examples
pythonindexingwhile-looppalindrome

How to check for palindrome using while loop python


I'm trying to check for a palindrome using a while loop and indexing, return True or False. I know this can be done much simpler using a for loop or even just one line: return num[::-1] == num
(num being the parameter inside the function) click for image of code here

I'd love to complete this with a while loop if anyone could shed some light on what im doing wrong here would be great:)

By the way a palindrome is a word or phrase which can be read the same the reversed way around, example: level, racecar, rotavator etc


Solution

  • def palindrome(s):
        i = 0
        while i <= len(s) / 2:
            if s[i] != s[-i - 1]:
                return False
            i += 1
        return True
    

    this should do it.