Function Practice problem 1012-9

Screen Link: https://app.dataquest.io/m/1012/python-fundamentals-practice-problems/9/the-closest-restaurant

My Code:

def close_res(x, y):
    fopen = open("restaurants.csv")
    fread = reader(fopen)
    reslis = list(fread)[1:]
    name_dist = {}
    for lis in reslis:
        dist = distance(x,y,int(lis[-2]),int(lis[-1]))
        name_dist[lis[0]] = dist
    closest_name = min(name_dist, key=name_dist.get)
    return closest_name

What I expected to happen: to return the corresponding key to the minimum value in the dictionary which supposed to be the closest name to the coordinates

What actually happened: returned a different name when used a coordinate x=10 and y=10

1 Like

hey @karimyousrymohamedom

Your code as such works fine. Can you please elaborate the error or IDE message that you get.

Hi @Rucha,

Actually it worked after i refreshed the page, thanks a lot

Hello,
Does not work for me with your exact code. what’s wrong?

Hey @rchatue, if refreshing the browser didn’t work, maybe you need to close and open the window again, the code worked fine once i did so

1 Like

Thank you anyway . i have tried everything but still got that :

Executing your function resulted in an error. Error: 'closest_restaurant' Input: [10, 10]
but its ok . I believe your solution is good :grinning:

Hi @rchatue

Can you post attach your code? In case we need to highlight this to Sahil for potential bug.
Thanks.

Here we go!
That was your exact code
def close_restaurants(x, y):
opened_file = open(“restaurants.csv”)
read_file = reader(opened_file)
reslis = list(read_file)[1:]
name_dist = {}
for lis in reslis:
dist = distance(x,y,int(lis[-2]),int(lis[-1]))
name_dist[lis[0]] = dist
closest_name = min(name_dist, key=name_dist.get)
return closest_name
print(close_restaurants(x, y))