简体   繁体   English

Python'builtin_function_or_method'对象不是可迭代错误

[英]Python 'builtin_function_or_method' object is not iterable error

i created this program which will replace a sentence with the words in its position and saves them as a separate file. 我创建了此程序,该程序将用其位置的单词替换一个句子并将其保存为单独的文件。 However when i run it i get this error: Traceback (most recent call last): File , line 12, in Positions = list2.index(word) 但是,当我运行它时,出现以下错误:追溯(最近一次调用最近):Files,第12行,位置= list2.index(word)

here is the code: 这是代码:

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()

could someone explain me what mistake i made in my code? 有人可以解释我在代码中犯了什么错误吗?

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

Without using the parentheses, you are actually assigning the method .split to the variable words , then trying to iterate over it, giving you the error. 不使用括号,实际上是将.split方法分配给变量words ,然后尝试对其进行迭代,从而给您带来错误。

You set words = words.split , but you aren't actually calling the method, so instead you are trying to iterate over the reference to that function. 您设置words = words.split ,但实际上并没有调用该方法,因此您尝试遍历对该函数的引用。 Call the method instead with words = words.split() 改为使用words = words.split()调用该方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python搜索中的'builtin_function_or_method'对象不可迭代'错误? - 'builtin_function_or_method' object is not iterable' error in python search? 'builtin_function_or_method' object 不可迭代 - 'builtin_function_or_method' object is not iterable 'builtin_function_or_method' 对象不是 Python 中的可订阅错误 - 'builtin_function_or_method' object is not subscriptable Error in Python Python错误:AttributeError:'builtin_function_or_method'对象没有属性 - Python Error: AttributeError: 'builtin_function_or_method' object has no attribute python错误:类型为“ builtin_function_or_method”的对象没有len() - python error: object of type 'builtin_function_or_method' has no len() python错误:builtin_function_or_method对象无法下标 - python error:builtin_function_or_method object is unsubscriptable TypeError: 'builtin_function_or_method' object is not subscriptable error in python 3 - TypeError: 'builtin_function_or_method' object is not subscriptable error in python 3 使用 os.walk 不可迭代 builtin_function_or_method' 对象 - builtin_function_or_method' object is not iterable using os.walk 显示TypeError:'builtin_function_or_method'对象在Odoo中不可迭代 - showing TypeError: 'builtin_function_or_method' object is not iterable in Odoo 使用max()时获取“'builtin_function_or_method'对象不可迭代” - Getting “'builtin_function_or_method' object is not iterable” when using max()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM