Exercise 16. Using strings in lists and functions - Code hardly works, but I'm still getting this error(NOW SOLVED)


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096#


I tried to do something different and it did try to print out the combined strings; however, I keep getting this error message saying "Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned 'Michael Lieberman' instead of 'xyza'".

I've been looking at some other resources for help outside of Codecademy and I felt that it didn't help much.


I thought it could just print the two strings in the list without using the result variable.


n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    for i in words:
        
        return " ".join(n)
print join_strings(n)


#2

Where could it possibly be getting any letters other than xyza from?


#3

It's from the list in n, I guess.


#4

And what should your function use?


#5

I went back to researching again after a long break until I found out what was missing. Thanks anyway, @ionatan.

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in words:
        result = result + i #or result += i
    return result
print join_strings(n)

I put the result variable back in the code, and instead of using .join(n), I was supposed to create the result variable again and concatenate the result variable with the i from the for loop, then return the result, thus it applies to the list and it adds the two strings together when it's printed. (I don't think it makes sense neither, bear with me until I try to improve the ability to explain things better.)


#6

Since you were doing the right operation but on the wrong data, all you needed to do was to change from n to what it was supposed to join, that was the difference in its behaviour, so that was the only thing you needed to change

"Does the right thing to the wrong data" -> "operate on the right data instead"


#7

So that means I was supposed to change the n to something else at print join_strings(n) or when I used .join()?


#8

No, your function was almost completely ignoring its argument. Calling it differently does not affect the correctness of the function.

Your function's purpose is to join the strings given to it.
Your function near ignores what is given to it, and joins something else instead.

It also has a loop that isn't a loop at all, because it never repeats.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.