Functions: Fundamentals Mission Practice Problems - 9. The Closest Restaurant (Function Scope issue)

I’m having an issue with the scope of the distance() function. The function code is given, but when I call this function, I run into errors. When I ignore the function and calculate the distance manually with the lines:

smallest_distance = math.sqrt((x - x_0)**2 + (y - y_0)**2)
and
distance = math.sqrt((x - x_2)**2 + (y - y_2)**2)

the code works fine. So the only issue here is when I use the defined function. But why…?

Screen Link:

My Code:

# provided input
x = 100
y = 100 
# answer to this input: LEADBETTERS II

from csv import reader
import math
def distance(x_1, y_1, x_2, y_2):
    return math.sqrt((x_1 - x_2)**2 + (y_1 - y_2)**2)

def closest_restaurant(x, y):
    # Create a list from the CSV file (header is excluded)
    opened_file = open('restaurants.csv')
    read_file = reader(opened_file)
    rows = list(read_file)[1:]
    
    # Initialize the coordinates/distance and the restaurant name
    x_0 = int(rows[0][-2])
    y_0 = int(rows[0][-1])
    smallest_distance = math.sqrt((x - x_0)**2 + (y - y_0)**2)
    # smallest_distance = distance(x, y, x_0, y_0) #doesn't work. Why???
    
    closest_name = ''
    
    # Loop over the rows and compute the smallest distance
    for row in rows[1:]:
        x_2 = int(row[-2])
        y_2 = int(row[-1])
        distance = math.sqrt((x - x_2)**2 + (y - y_2)**2)
        # distance = distance(x, y, x_2, y_2)
        # Same problem again. I can't use the distance(0) method here, why not???
        
        if distance < smallest_distance:
            smallest_distance = distance
            closest_name = row[0]
            
    return closest_name

closest_restaurant(x, y)

What I expected to happen:
I expected to be able to use the distance() function to calculate the distance.

What actually happened:
I get an error message telling me that the variables resulting from calling the distance() function are undefined. I called this function twice: at line 21 and line 30. So it means the function call didn’t work. But am I not doing the calculation within the function scope? I don’t understand why I can’t call the function here.

<ipython-input-1-34e8ca735ac0> in closest_restaurant(x, y)
     21     y_0 = int(rows[0][-1])
     22     # smallest_distance = math.sqrt((x - x_0)**2 + (y - y_0)**2)
---> 23     smallest_distance = distance(x, y, x_0, y_0) #doesn't work. Why???
     24 
     25     closest_name = ''

UnboundLocalError: local variable 'distance' referenced before assignment

I guess I don’t have a good understanding of function scope yet. Any help would be appreciated.
This was my first message on the board, Dataquest noob here :smiley: I checked all other 4 topics on this problem, so I hope this question is not redundant.

You get that error because you have a function called distance() as well as a normal variable called distance in your code -

distance = math.sqrt((x - x_2)**2 + (y - y_2)**2)

That’s why there is a confusion of scope because once inside the closest_restaurant() function, distance (the variable) has closest_restaurant()'s scope. So, it detects that you are trying to use distance inside the function closest_restaurant() but that distance has not yet been defined in that function and hence the error.

Be careful about naming your variables and functions and you shouldn’t face an issue.

1 Like

How did I not see that… if it was a snake, it would have bitten me.
Thanks a lot! :handshake: