gpt4 book ai didi

Python 3.7 内置名称错误

转载 作者:行者123 更新时间:2023-12-01 07:01:48 24 4
gpt4 key购买 nike

现在我只是在学习列表之类的基础知识,但是在编写代码时遇到了问题:

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: name nums1 is not defined

我尝试使用缩进,但没有帮助。

最佳答案

您似乎没有缩进函数的内容。试试这个:

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)

关于Python 3.7 内置名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58596331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com