0

Find the error in the code As it always shows Not a palindrom

#Palindrom check a=str(input("Please enter a word: ")) b=list(a) l=len(a) r= " " for x in range(l-1,-1,-1): r+=b[x] c=str(r) if c==a : print("It's a palindrom.") else: print("It's not a palindrom.")

16th Sep 2019, 2:38 AM
Jaival Buchara
Jaival Buchara - avatar
5 odpowiedzi
+ 5
I guess you know the easier test 🙂 if a == a[::-1]:
16th Sep 2019, 9:45 AM
David Ashton
David Ashton - avatar
+ 3
Well the input() returns a string so the str() in line 1 is not needed. You can directly do r+=a[x] so creating b is not necessary. r is already a string so you don't need c=str(r). Finally, initialize r as an empty string, not one with a space, i.e. r="", not r=" "
16th Sep 2019, 3:31 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
Igor Kostrikin He is right on that aspect as the endpoint is excluded from the range.
16th Sep 2019, 3:31 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
May be range(l-1,0,-1) ?
16th Sep 2019, 3:00 AM
Igor Kostrikin
Igor Kostrikin - avatar
16th Sep 2019, 3:44 AM
Kunal Solanke
Kunal Solanke - avatar