def palindrome(w):
if len(w)==0 or len(w)==1:
p = 'yes'
return p
else:
if w[0]==w[-1]:
del w[0]
del w[-1]
palindrome(w)
else:
p = 'no'
return p
s=input()
w=list(s.replace(" ",""))
print(palindrome(w))
when input is not a palindrome, the code works fine. but when the input is a palindrome. it is returning the value none instead of yes. why is that?
ps:- I'm a beginner-level programmer. I'm still learning. so please be kind enough to explain me more straightforwardly.
You will have to propagate the result of your palindrome(w)
call:
return palindrome(w)
If a function ends without a return
being executed, the function will automatically return the value None
. Thus, e.g. if you call palindrome(["a", "b", "a"])
, you will evaluate the subcall palindrome(["b"])
, then discard its result, and return None
for the value of palindrome(["a", "b", "a"])
. If you use return palindrome(w)
, then the value of palindrome(["a", "b", "a"])
will become the value of palindrome(["b"])
, which is the desired logic.
The following are not errors, just good practices.
It is probably better to make the function more general. Instead of making a palindrome function that can only handle lists, why not make it so it can handle any sequence, including strings directly? However:
del w[0]
del w[-1]
This does not work on strings, and will give you a TypeError: 'str' object doesn't support item deletion. You cannot use del
to remove characters in a string: Python strings are immutable. You have to create a new string without the first and last character instead:
w = w[1:-1]
Now the function will work both for palindrome([1, 2, 3])
and palindrome("abc")
.
Finally, it would be better to use the standard Boolean type for "yes" and "no". If you need to display yes
or no
to the user, implement this logic outside the function that performs the test, preferably at the very last moment before you need to display it.
The final form:
def palindrome(w):
if len(w)==0 or len(w)==1:
return True
else:
if w[0]==w[-1]:
w = w[1:-1]
return palindrome(w)
else:
return False
s = input()
w = s.replace(" ","")
if palindrome(w):
print("yes")
else:
print("no")