Search code examples
pythonpython-3.xpalindrome

Whether the input is a palidrome or not


So, i am writing a code to check whether the entered string is a palindrome or not, i have written the following code, but its not working properly, for e.g, if i input "race" it still says TRUE, although it should say FALSE, Please help. This is the code

string = input("Please enter any word: ")
a = 0
string_length = len(string)
for string_index in range(string_length-1, -1, -1):
    character = string[string_index]
    if string[a] == character:
        a = a + 1
        b = "TRUE"
    else:
        b = "FALSE"
print(b) 

Solution

  • The correct code:

    a = 0
    string_length = len(string)
    for string_index in range(string_length-1, -1, -1):
        character = string[string_index]
        if string[a] == character:
            a = a + 1
            b = "TRUE"
        else:
            b = "FALSE"
            break;  # this line was missing
    print(b)