Python 检查字符串是否为回文时出现递归错误
我写下了这段代码,它检查字符串是否是回文的,但在执行时,输出显示“递归错误”。我如何解决这个问题Python 检查字符串是否为回文时出现递归错误,python,python-3.x,python-3.7,Python,Python 3.x,Python 3.7,我写下了这段代码,它检查字符串是否是回文的,但在执行时,输出显示“递归错误”。我如何解决这个问题 def palindrome(string): if len(string) < 1: return True else: if string[0] == string[-1]: return palindrome(string[::-1]) else: return False
def palindrome(string):
if len(string) < 1:
return True
else:
if string[0] == string[-1]:
return palindrome(string[::-1])
else:
return False
string = input("Enter a string:")
if palindrome(string)==True:
print("String is palindrome")
else:
print("String is not palindrome:")
def回文(字符串):
如果len(字符串)<1:
返回真值
其他:
如果字符串[0]==字符串[-1]:
返回回文(字符串[:-1])
其他:
返回错误
字符串=输入(“输入字符串:”)
如果回文(字符串)=真:
打印(“字符串是回文的”)
其他:
打印(“字符串不是回文:”)
您有两个错误
第一个是退出条件,长度为1的字符串应该是回文
第二个错误是string[:-1]
返回字符串,但返回的是相反的,没有减小大小。您希望检查字符串减去第一个和最后一个元素,如下所示:string[1:-1]
def palindrome(string):
if len(string) < 2:
return True
else:
if string[0] == string[-1]:
return palindrome(string[1:-1])
else:
return False
string="tacocat"
if palindrome(string): # You should avoid using "if x == True"
print("String is palindrome")
else:
print("String is not palindrome:")
def回文(字符串):
如果len(字符串)<2:
返回真值
其他:
如果字符串[0]==字符串[-1]:
返回回文(字符串[1:-1])
其他:
返回错误
string=“tacocate”
if回文(字符串):#应避免使用“if x==True”
打印(“字符串是回文的”)
其他:
打印(“字符串不是回文:”)
如注释中所述,您还可以通过以下方式检查它是否是回文string==string[::-1]
如果您不想执行递归您有两个错误
def palindrome(string):
if len(string) < 2:
return True
else:
if string[0] == string[-1]:
return palindrome(string[1:-1])
else:
return False
string=input(“Enter the string”)
if palindrome(string)==True:
print("String is palindrome")
else:
print("String is not palindrome:")
第一个是退出条件,长度为1的字符串应该是回文
第二个错误是string[:-1]
返回字符串,但返回的是相反的,没有减小大小。您希望检查字符串减去第一个和最后一个元素,如下所示:string[1:-1]
def palindrome(string):
if len(string) < 2:
return True
else:
if string[0] == string[-1]:
return palindrome(string[1:-1])
else:
return False
string="tacocat"
if palindrome(string): # You should avoid using "if x == True"
print("String is palindrome")
else:
print("String is not palindrome:")
def回文(字符串):
如果len(字符串)<2:
返回真值
其他:
如果字符串[0]==字符串[-1]:
返回回文(字符串[1:-1])
其他:
返回错误
string=“tacocate”
if回文(字符串):#应避免使用“if x==True”
打印(“字符串是回文的”)
其他:
打印(“字符串不是回文:”)
如注释中所述,如果您不想执行递归,还可以通过以下方式检查它是否为回文string==string[::-1]
def palindrome(string):
if len(string) < 2:
return True
else:
if string[0] == string[-1]:
return palindrome(string[1:-1])
else:
return False
string=input(“Enter the string”)
if palindrome(string)==True:
print("String is palindrome")
else:
print("String is not palindrome:")
如果len(字符串)<2:
返回真值
其他:
如果字符串[0]==字符串[-1]:
返回回文(字符串[1:-1])
其他:
返回错误
字符串=输入(“输入字符串”)
如果回文(字符串)=真:
打印(“字符串是回文的”)
其他:
打印(“字符串不是回文:”)
def回文(字符串):
如果len(字符串)<2:
返回真值
其他:
如果字符串[0]==字符串[-1]:
返回回文(字符串[1:-1])
其他:
返回错误
字符串=输入(“输入字符串”)
如果回文(字符串)=真:
打印(“字符串是回文的”)
其他:
打印(“字符串不是回文:”)
这部分:string[:-1]
是检查某个内容是否为回文所需的全部内容,您只需返回string==string[:-1]
即可检查字符串是否前后相同。如果这应该是一个练习,那么执行[:-1]根本没有帮助这可能与您的问题无关,但是你问题中的代码有缩进错误。我修正了它们,但请确保你的实际代码和问题有相同的缩进。这部分:string[:-1]
是检查某个内容是否为回文所需的全部内容,你只需return string==string[::-1]
,它将检查字符串是否前后相同。如果这是一个练习,执行[::-1]根本没有帮助这可能与您的问题无关,但问题中的代码有缩进错误。我修正了它们,但请确保您的实际代码和问题具有相同的缩进。