Loops code challenge number 2 - greetings

Hi, here is the page where I don’t get the difference between my output, and the one expected.
To simplify, here is a screen capture :

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/greetings

You could look at the difference in the output and ask yourself what you must have done to make that difference happen.

You’ve got quite a lot of information in the output you incorrectly produced, it says a lot about what was wrong and what you did to get it.

Well you mean that my output :
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

is different from the expected output:
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

sorry but I dont see any difference between these 2 outputs.

I don’t understantd why it says that my output is :
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’, ‘Hello, Owen’, ‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

when in the right window I have the output I wrote at the beginning of this post.

I have just checked using Wings Pro 7, and my output is correct :

[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

So there is a problem at your side for the check.

Can someone give a look at this please, I am stuck and cant pass to the challenge n°3, even if my code is correct.

Edit : ok, I asked for the solution of the challenge, and the code is very close to mine, the only difference is that the empty list is created inside the loop.
And you know what, after that, I tried to submit my code again, and this time it has worked, so there is clearly a bug for this challenge, I am using Chrome.

@ionatan, please read more carefully my post next time, you ll see that the error message return by codeacademy is wrong compare to the code submitted.

That’s actually a huge difference. Consider the SCT error message from your first post. Can you see how having greetings = [] outside the function produced the result shown in the error message?

If you were tasked with writing the Submission Correctness Test for this exercise, how would you check whether the learner had written a function that produces expected results? Most likely you’d want to test it with more that just a single input value, and more than one time. Without testing more than once, a learner could cheat by doing simply this:

def add_greetings(names):
    return ["Hello, Owen", "Hello, Max", "Hello, Sophie"]

Since we know what the return value is supposed to be for the test, we simply return it. This code only returns the correct output if the input never changes from ["Owen", "Max", "Sophie"].

In order to ensure that learners have actually written a function that performs the specified task, multiple varied inputs are used. Given the output shown in the error message, we can tell what inputs were used, and produce the same output:

greetings = []

def add_greetings(names):
    for name in names:
        greetings.append("Hello, " + name)
    return greetings
    
print(add_greetings(["Owen", "Max", "Sophie"]))
print(add_greetings(["Owen"]))
print(add_greetings(["Owen", "Max", "Sophie"]))

Output:

[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’, ‘Hello, Owen’]
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’, ‘Hello, Owen’, ‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

So, what happens if we run the same tests after moving greetings = [] inside the function (not inside the loop - you could try that to see what would happen):

def add_greetings(names):
    greetings = []
    for name in names:
        greetings.append("Hello, " + name)
    return greetings
    
print(add_greetings(["Owen", "Max", "Sophie"]))
print(add_greetings(["Owen"]))
print(add_greetings(["Owen", "Max", "Sophie"]))

Output:

[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]
[‘Hello, Owen’]
[‘Hello, Owen’, ‘Hello, Max’, ‘Hello, Sophie’]

Now we have the expected output for each test.

Thank you for these explanations, I understand now why the empty list inside the function is important, and why at the end the output could be different according to the input.

1 Like