def ispalindrome(s):
"""Assumes s is a str
returns true if s is a ispalindrome
punctuation marks, blanks, and capitals are igored """
#s=raw_input("Please enter a word to check if it is a palindrome")
def tochars(s):
s=s.lower()
letters=''
for c in s :
if c in 'abcdefghijklmnopqrstuvwxyz':
letters=letters+c
return letters
def ispal(s):
print ' ispalindrome called with' , s
if len(s)<=1 :
print "about to return to from base case"
return True
else :
answer = s[0] == s[-1 ] and ispal (s[1:-1])
print "about to return ",answer,"for" ,s
return answer
return ispal(tochars(s))
def testpal():
print 'try doggod'
print ispalindrome('doggod')
当我运行上面的代码时,它被编译为seanlessly但没有返回任何内容。没有错误消息,但程序没有打印任何内容。请给出一些建议。
答案 0 :(得分:3)
该行
return ispal(tochars(s))
缩进太远。
这使它成为ispal(s)
函数的一部分,因此它永远不会被调用。
您的缩进通常非常不一致(有时一个,有时两个,有时三个空格)。如果你不解决这个问题,你将继续遇到这样的错误。
此外,您永远不会调用testpal()
函数。
如果您解决了所有这些问题,它运行良好:
def ispalindrome(s):
def tochars(s):
s = s.lower()
letters = ''
for c in s:
if c in 'abcdefghijklmnopqrstuvwxyz':
letters = letters + c
return letters
def ispal(s):
print 'ispalindrome called with', s
if len(s) <= 1:
print "about to return to from base case"
return True
else:
answer = s[0] == s[-1 ] and ispal (s[1:-1])
print "about to return ", answer, "for", s
return answer
return ispal(tochars(s))
def testpal():
print 'try doggod'
print ispalindrome('doggod')
testpal()