jeudi 19 novembre 2020

Python not returning True as it should [duplicate]

def palindromes():
    # Excersise 1
    # Returns True if two input strings [presenting two words] are palindromes.

    string1 = input(str("Type a word: ")) #input for string 1
    string2 = input(str("Type another word: ")) #input for string 2

    string1rev = string1 [::-1] #reversing string 1
    string2rev = string2 [::-1] #reversing string 2
    
    print(string1, " : " + string1rev) 
    print(string2, " : " + string2rev)
    
    if string1 == string1rev and string2 == string2rev:
        # This checks if the strings match when reverse, then returns true.
        return True

It all runs as it should, but doesn't return true as I'd expect.

I've also tried assigning a = False, then reassigning it in the if statement with a = True and return a.

The error that I'm getting is that the code is inaccessible. I'd assume that this is because the if statement doesn't run properly, but I don't know how to fix it.

Aucun commentaire:

Enregistrer un commentaire