python 如何调试小程序

how to debug small programs in python

我的代码有问题。我有一个句子,这个句子有5个字母的回文词。句子是:the level rotor is in the radar。如您所见,回文词是:水平仪、转子和雷达。首先,我将单词分成 5 个字母,如下所示:thele helev eleve level,依此类推。

我的代码是这样的:

test = "the level rotor is in the radar"
data = []

for i in range(len(test) - 5):
    data.append(test[i:i+6])
    print(data)

def siqpalindromics(pal):
    return pal == pal[::-1]

pal = "data"
anpal = siqpalindromics(pal)

if anpal:
    print("it is palindromic"), print(pal)
else:
    print("it isnt palindromic")

我要打印例如:

thele it isnt palindromic
helev it isnt palindromic
eleve it isnt palindromic
level it is palindromic

提前致谢

您可以使用 for 循环来检查每个提取的单词是否为回文。

def is_palindrome(s):
    return s == s[::-1]

data = "the level rotor is in the radar"
data = data.replace(' ', '')
words = []
word_length = 5
for i in range(len(data) - word_length + 1):
    words.append(data[i:i+word_length])

for word in words:
    if is_palindrome(word):
        print(f'{word} is palindromic')
    else:
        print(f'{word} is not palindromic')