Nameerror : is not defined in python3

Asked

Viewed 1,063 times

2

I have a simple problem, when I run this function it returns an error :

Traceback (Most recent call last): file.py3 on line ? , in getUserOutputs userOutput = _runwwmdh(testInputs[i]) file.py3 on line 15, in _runwwmdh Return Aux1 + aux2 Nameerror: name 'Aux1' is not defined

What problem do I face ? and how do I solve ?

def perfectCity(departure, destination ):
    aux1 = 0
    aux2 = 0



    if departure[0] > destination[0]:
        aux1 = departure[0] - destination[0]
    else:
        aux1 = destination[0] - departure[0]
    if departure[1] > destination[1]:
        aux2 = departure[1] - destination[1]
    else:
        aux2 = destination[1] - departure[1]
return aux1 + aux2
  • 2

    First confirm that the indentation of the code here in the question matches the one in your editor.

1 answer

1


The only problem I see in the code is that the indentation of the return aux1 + aux2 is misaligned.

Python, unlike other languages (e.g. PHP), space is important, and this is what defines language blocks, not braces.

This example works for me.

def perfectCity(departure, destination ):
    aux1 = 0
    aux2 = 0

    if departure[0] > destination[0]:
        aux1 = departure[0] - destination[0]
    else:
        aux1 = destination[0] - departure[0]

    if departure[1] > destination[1]:
        aux2 = departure[1] - destination[1]
    else:
        aux2 = destination[1] - departure[1]

    return aux1 + aux2

perfectCity([1, 2, 3], [3, 2, 1])

''' retorna 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.