Python3可变减速

2024-10-01 13:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开始学习Python,我想通过解决Leetcode问题来实践它。 我选择这个开头:https://leetcode.com/problems/two-sum/

在编辑器中,我选择了Python3,它提供了以下起始代码:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

我已将此代码复制到Visual Studio代码中,并使用一些命令完成了此代码,但当我尝试运行它时,出现以下错误:

Exception has occurred: NameError
name 'List' is not defined
  File "F:\Development\TestConsolePy\solution.py", line 2, in Solution
    def twoSum(self, nums: List[int], target: int) -> List[int]:
  File "F:\Development\TestConsolePy\solution.py", line 1, in <module>
    class Solution:

我的Windows计算机上安装了Python 3.8.5

从我在网上看到的情况来看,python通常没有类型声明——但在我的例子中,我想我在选择python3并在我的机器上运行python3时肯定遗漏了一些东西。。。(如果我使用相同的命令并将它们粘贴到Leetcode生成的python代码中,那么它就不起作用了)

我误解了什么


Tags: 代码命令selftargetdeflistclassfile