Python 错误:TypeError: 'list' 对象不可调用

-1 投票
1 回答
6550 浏览
提问于 2025-04-18 04:31

我有一段代码:

count_words = input("Put in your favorite word, and then press the hidden button(SPOILER: IT'S ENTER!):")
split_Geonotes = Geonotes.replace ("\n", " "). split(".")
print (" Total number of words:", len (Geonotes.split() ) )

print("Total number of sentences:", len (Geonotes.split(".") ) )
print("Total number of periods:", Geonotes.count(".") )
print("you typed:", count_words)
print("There are:", Geonotes.count(count_words), "instances of", count_words)
split_Geonotes.sort()
print(split_Geonotes)
print("The number of elements in the HTML code:", len (split_Geonotes) )

for i in range(10):
    print(i)
    print(split_Geonotes(i) )

这段代码导致了标题中提到的错误。根据完整的错误信息,问题出现在 print(i) 这一行。有人能告诉我哪里出错了吗?谢谢。

1 个回答

0

看起来你在最后一行想要访问列表 split_Geonotes 的某个元素。你需要用方括号 [...] 来做到这一点:

print(split_Geonotes[i])

而圆括号 (...) 是用来调用函数的;当你把它们放在 split_Geonotes 后面时,Python 会尝试把这个列表当作一个函数来调用:

>>> lst = [1, 2, 3]
>>> lst(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> lst[0]
1
>>>

撰写回答