Artem Pankratov
Artem Pankratov

Reputation: 13

Python 3.7 builtins.NameError

right now I'm just studying basic things like list and such, but I encountered a problem while writing my code:

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)

For some reason, it says

builtins.NameError: name nums1 is not defined

I tried to play with the indents, but it didn't help.

Upvotes: 1

Views: 124

Answers (1)

Dan
Dan

Reputation: 45752

It looks like you haven't indented the contents of the function. Try this:

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)

Upvotes: 1

Related Questions