Python';内置函数或方法';对象是不可忽略的错误

Python';内置函数或方法';对象是不可忽略的错误,python,traceback,Python,Traceback,我创建了这个程序,它将在句子的位置用单词替换句子,并将它们保存为一个单独的文件。但是,当我运行它时,会出现以下错误: 回溯(最近一次呼叫最后一次): 文件,第12行,在 位置=列表2.索引(word) 代码如下: UserSentence = input("enter sentence:").lower() words = UserSentence words = words.split() number = 0 list1 = [] list2 = " " for Position in w

我创建了这个程序,它将在句子的位置用单词替换句子,并将它们保存为一个单独的文件。但是,当我运行它时,会出现以下错误: 回溯(最近一次呼叫最后一次): 文件,第12行,在 位置=列表2.索引(word)

代码如下:

UserSentence = input("enter sentence:").lower()
words = UserSentence
words = words.split()
number = 0
list1 = []
list2 = " "

for Position in words:
    if Position not in list1:
        list1.append(Position)
for word in words:
        Positions = list2.index(word)
        list2+=str(Positions+int("1"))
        list2 +=("")
list1str += ";".join(list1)

file = open ("myfile.txt","w")
file.write(sentence)
file.write(list1str)
file.write(list2)
file = open ("myfile.txt", "r")
print (file.read())
file.close()
有人能解释一下我在代码中犯了什么错误吗

UserSentence = input("enter sentence:").lower()
words = UserSentence
words = words.split() # actually call the function

在不使用括号的情况下,实际上是将方法
.split
赋给变量
words
,然后尝试对其进行迭代,从而给出错误。

设置了
words=words.split
,但实际上并没有调用该方法,因此您尝试对该函数的引用进行迭代。使用
words=words.split()
words=words.split
调用该方法。split应该是
words=words.split()
。谢谢,但现在它在行位置=列表2上显示了一个回溯错误。索引(word)值错误:未找到子字符串为什么会这样?对于words-in-words>
的第一次迭代中,
list2
只包含一个空格,因此
list2.index(word)
将失败,除非
word
也只是一个空格。但这是一个词,不是一个空格。顺便说一句,
list2
不是一个列表,所以你不应该称它为列表。我怎样才能使word只是一个空格呢?很简单:
word=”“
。但你为什么要这么做?它会使错误消失,但不会使程序正常工作。