Python. Making a function that tests if a word is a palindrome recursively -
i'm trying make program tests if word palindrome using recursive function. pretty have working i'm having trouble getting move on next letter if first , last same.
word = input("enterword") word = word.lower() def palindrom(word): if len(word) == 1 or len(word) == 0: return 0; if word[0] == word[-1]: print(word[0], word[-1]) palindrom(word); else: return 1; test = palindrom(word) if test == 0: print("yes") elif test == 1: print("no")
so right tests if first , last letter same , if so, should run function again. need have check word[1] , word[-2] i'm having trouble. tried splitting word , popping letters kept looking @ list length of 1. if there way get length of whole split list, work well.
you're missing return statement when call method recursively , correct slice:
def palindrom(word): if len(word) == 1 or len(word) == 0: return 0 if word[0] == word[-1]: print(word[0], word[-1]) return palindrom(word[1:-1]) else: return 1
Comments
Post a Comment