NameError: is not defined in python3

0

I have a simple problem, when I execute 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 it?

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
    
asked by anonymous 15.08.2018 / 01:42

1 answer

0

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

Python, unlike other languages (eg PHP), space is important, and this is what defines the 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
    
15.08.2018 / 01:56