【发布时间】:2021-09-18 01:29:35
【问题描述】:
我在下面的屏幕截图中有代码和错误。但不知道为什么它给我这个错误。请帮忙。
def add_node(self, id: str, name: str) -> None:
"""
add a tuple (id, name) representing a node to self.nodes if it does not already exist
The graph should not contain any duplicate nodes
"""
node = (id, name)
if node not in self.nodes:
self.nodes.append(node)
return self.nodes
【问题讨论】:
-
您使用的是什么版本的 Python? Python 3 中引入了类型注释,但使用旧的解释器可能会给你这样的错误。
-
这是有效的 Python,在我的机器上运行得很好。这是在某些不允许您使用类型提示的第 3 方 IDE 中吗?
-
似乎您的编辑器正在使用 Python 2(或真正的旧 Python 3)运行。这在 Python 3 中没有任何问题。因此您需要更新您的编辑器以使用正确的版本运行它。
-
@Daniel 从标签来看,它看起来像 OP 使用 Atom,输出窗格看起来像
script包,它使用外部解释器。 -
Python 2 的问题很重要。我对 Python 比较陌生,一直忘记 Python 2 是一回事。
标签: python atom-editor