大家好,我这里有一小段代码让我很头疼。在这个函数中,我定义了一个名为的列表word
,然后列表突然变成了一个字符串。我不知道这怎么可能:
def find_best_shift(wordlist, text):
words = []
word = []
for x in range(0, 27):
apply_shift(text, x)
for character in text:
print (type(word))
if (character in ALPHABET) or (character in alphabet):
word.append(character)
else:
wor = ''.join(word)
words.append(wor)
word = []
for word in words:
y = is_word(wordlist, word)
if y == True:
return x
你们注意到打印声明print(type(word))
。使用此语句,我得到以下输出:
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'str'>
然后它给了我以下错误:
File "C:\Users\usuario\Documents\Python Projects\ps4\ps4.py", line 264, in find_best_shift
word.append(character)
AttributeError: 'str' object has no attribute 'append'
我将不胜感激任何帮助!