+ 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?

28th Apr 2020, 4:12 AM
Mons Joseph
Mons Joseph - avatar
3 Answers
+ 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)
28th Apr 2020, 8:54 AM
Lothar
Lothar - avatar
+ 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.
28th Apr 2020, 6:35 AM
Lothar
Lothar - avatar
+ 2
Does your program deal effectively with palindrome. Instead you can try reverse the string and check the difference.
28th Apr 2020, 4:31 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar