我正在检查一个单词是否是回文,我似乎无法让代码响应。我觉得听起来不错,但显然我遗漏了什么。有人能指出那可能是什么吗?在
def reverse(usrWrd, index):
newWord = ""
if index == len(usrWrd):
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1)
def main():
usrWrd = input("please enter a word to check for palindrome-ness:")
result = reverse(usrWrd, 0)
if result == usrWrd:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
main()
如前所述局部变量是问题,可能是你可以把局部变量一起发送。典型的尾部递归实现
希望有帮助!在
或者对于那些喜欢更简洁代码的人:
^{pr2}$相关问题 更多 >
编程相关推荐