使用Spyder Python 3.6此代码不会执行,表示ispal方法未定义。但是,当我运行它并首先放入一个整数(比如我的字符串输入= 0)时,它会在运行之后识别该方法。似乎我必须先通过一个分支,而不是首先调用该方法。感谢批评。Python代码不会在第一次运行时执行
s = input('enter a string: ')
s1 = s
s1 = s1.lower()
s1 = s1.replace(',', '')
s1 = s1.replace(' ', '')
if s1.isalpha():
if ispal(s1) == True:
print(s,' is a palindrome')
else:
print(s,' is not a palindrome')
else:
print('you entered illegal chars')
def ispal(s1):
if len(s1) <= 1:
return True
else:
#if the first and last char are the same
#and if all
return s1[0] == s1[-1] and ispal(s1[1:])
您在调用它之后定义了该功能 – TGKL
感谢您的帮助! – femmebot