所以我最近实现了一个代码来检查一个单词是否是回文。
def isPalindrome():
string = input('Enter a string: ')
string1 = string[::-1]
if string[0] == string[(len(string)-1)] and string[1:(len(string)-2)] == string1[1:(len(string)-2)]:
print('It is a palindrome')
else:
print('It is not a palindrome')
isPalindrome()
我想知道是否有人可以给我关于简化代码的提示。
编辑 - 如果我要使用 statements 使函数成为迭代函数string == string1
,我将如何停止无休止的 while 循环?我需要计数来停止while循环吗?