Exercise 3 - get_country in Programming Assignment

Exercise 3 - get_count will not be passed by
w3_unittest.test_get_country(get_country)

w3_unittest.test_get_country(get_country) expects similarity as well as country.

END CODE HERE

return country ←

return country, similarity

The above change worked well although the return line is out of our practice.

C1_W3_Assignment.ipynb

1 Like

It looks like you missed the point of the comments that were given in the template code. Here’s that section of the function as it was originally given to you in the clean notebook:

            # if the cosine similarity is more similar than the previously best similarity...
            if cur_similarity > similarity:

                # update the similarity to the new, better similarity
                similarity = None

                # store the country as a tuple, which contains the word and the similarity
                country = None

    ### END CODE HERE ###

    return country

You can either modify the return statement or you can follow the suggestion that they gave you to make country a python tuple containing two elements.

Note that the “docstring” of the function also specified that:

def get_country(city1, country1, city2, word_embeddings, cosine_similarity=cosine_similarity):
    """
    Input:
        city1: a string (the capital city of country1)
        country1: a string (the country of capital1)
        city2: a string (the capital city of country2)
        word_embeddings: a dictionary where the keys are words and values are their emmbeddings
    Output:
        country: a tuple with the most likely country and its similarity score
    """
    ### START CODE HERE ###

Modifying the values passed by the return statement is a rather poor idea, because the rest of the course (including the grader) is not expecting the function to return two values instead of a tuple.

1 Like

It’s an excellent point that you are “playing with fire” when you change the definition of one of the graded functions. It should never be necessary to do that. If you do decide to “go there”, you have to be very careful to do it in a way that is compatible with the original definition, because the grader and all the test cases are going to assume the original definition. Meaning that the only changes you can make without guaranteed disaster would be to add additional “keyword” parameters that default appropriately.

But in this very specific case, it turns out that you can handle the return values either way, because of the way python handles multiple return values. It turns out that what it does is create a “tuple” of all the return values when there is more than one. Here’s some sample code to demonstrate what happens:

def toyFunction():
    answer1 = 42
    answer2 = "answer cloudy, ask again later"
    return answer1, answer2

toyAnswer = toyFunction()
print(f"type(toyAnswer) {type(toyAnswer)}")
print(f"len(toyAnswer) {len(toyAnswer)}")
toyAnswer1, toyAnswer2 = toyAnswer
print(f"type(toyAnswer1) {type(toyAnswer1)}")
print(f"type(toyAnswer2) {type(toyAnswer2)}")
type(toyAnswer) <class 'tuple'>
len(toyAnswer) 2
type(toyAnswer1) <class 'int'>
type(toyAnswer2) <class 'str'>

@paulinpaloalto Thank you for your comment. I missed the comment. It was a good practice to me. Thanks,

1 Like