Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.7 builtins.NameError_Python_Error Handling - Fatal编程技术网

Python 3.7 builtins.NameError

Python 3.7 builtins.NameError,python,error-handling,Python,Error Handling,现在我正在学习一些基本的东西,比如列表之类的,但是我在编写代码时遇到了一个问题: from typing import List def greatest_difference(nums1: List[int], nums2: List[int]) -> int: """Return the greatest absolute difference between numbers at corresponding positions in nums1 and nums2

现在我正在学习一些基本的东西,比如列表之类的,但是我在编写代码时遇到了一个问题:

from typing import List


def greatest_difference(nums1: List[int], nums2: List[int]) -> int:
    """Return the greatest absolute difference between numbers at
    corresponding positions in nums1 and nums2.

    Precondition: len(nums1) == len(nums2) and nums1 != []

    >>> greatest_difference([1, 2, 3], [6, 8, 10])
    7
    >>> greatest_difference([1, -2, 3], [-6, 8, 10])
    10
    """
difference = 0
diff_over_term = 0
for i in range(len(nums1)):
    diff_over_term = abs(nums1[i] - nums2[i])
if diff_over_term > difference:
        difference = diff_over_term
print(difference)
出于某种原因,它说

builtins.NameError:未定义名称
nums1


我试着玩缩进,但没用

看起来您还没有缩进函数的内容。试试这个:

from typing import List


def greatest_difference(nums1: List[int], nums2: List[int]) -> int:
    """Return the greatest absolute difference between numbers at
    corresponding positions in nums1 and nums2.

    Precondition: len(nums1) == len(nums2) and nums1 != []

    >>> greatest_difference([1, 2, 3], [6, 8, 10])
    7
    >>> greatest_difference([1, -2, 3], [-6, 8, 10])
    10
    """
    difference = 0
    diff_over_term = 0
    for i in range(len(nums1)):
        diff_over_term = abs(nums1[i] - nums2[i])
    if diff_over_term > difference:
        difference = diff_over_term
    return difference

# and now call your function, notice how these lines aren't indented, that means they are not part of the function definition
list_a = [1, 2, 3]
list_b = [6, 8, 10]
print(greatest_difference(list_a, list_b)

您需要缩进作为函数一部分的代码行
nums1
是函数的参数,仅在该范围内定义。由于未缩进以下行,因此它们也不在函数范围内,因此未定义
nums1
。同样,不相关,但您应该重新启动并且
int
no?——不要编辑问题中的源代码;您修复了OP试图诊断的错误。