Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Palindrome Iterative checker in python

Tags:

python

I wrote a palindrome iterative method and it returns correct result except for when the string is arena or taxi cat. It returns true for both where it has to return false.

def palIterative(n):
    for i in range(0, int(len(n)/2)):
        if n[i] != n[len(n) - i - 1]:
            return False
            break
        return True
like image 903
Rutvi Gandhi Avatar asked Apr 18 '26 17:04

Rutvi Gandhi


1 Answers

You don't need break after the return False statement. Return will break out of a function. The problem though is your return True statement has one too many tabs. The return True statement should be after the for loop has completed.

like image 183
phil Avatar answered Apr 21 '26 09:04

phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!