+ 1
Please help me with my palindrome checher..
user_input=input('enter any word : ') x=1 y=0 if user_input[-x]==user_input[y] : print('This word is a palindrome') else: print('This word is not a palindrome') x+=1 y+=1 What shoul i do to increment the values of x and y?
3 Réponses
+ 3
Here is your code slightly reworked. Your code might not be the shortest one, but it demonstrates that there are other approaches off the road and mainstream. Really nice idea.
inp = input('enter any word: ')
x=1
y=0
msg = 'This word is a palindrome'
while y <= len(inp) // 2:
if inp[-x] != inp[y]:
msg = 'This word is NOT a palindrome'
break
x+=1
y+=1
print(msg)
+ 3
Your code does only check the first and last character of input. To detect a palindrome you have to check the complete word. Therefore the code has to use a loop and to modify the variables x and y in each iteration. You have also to make the code run with an even or odd number of characters from input.
+ 2
Does your program deal effectively with palindrome.
Instead you can try reverse the string and check the difference.