Using strings in lists in functiions


#1

Help! I have been stuck here for two days

I have used every suggestion I saw in the previous related posts and I still get some errors.
Is there something, I am doing wrong?

print join_strings(n)

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


#2

Look here:

Why slice from the second word in n? You need to join both words, not the just last name itself.


#3

I got that right. When I changed it, it still gave an error. Says "your code looks a bit off"


#4

See this?:

The words argument is what is going to hold n when calling it here:

So why call n when assigning to result?

If you're having any more problems, you can post your new code :slight_smile:.


#5

Here is my new code. Still shows some errors. I really don't understand it

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in words:
result = i + n
return result

print join_strings(words)


#6

So, on line 3, we're looping through words (which is n since you called it below). Now what we have looped through (word) is in i, it won't show as a list anymore, its now:

Michael
Lieberman

Since the instructions ask for us to append each word to result, we need to add them. See how I highlighted word and result? I'm hinting what to add there....

Look here:

See where our return keyword is placed? It's only going to return the first loop (first word in n which is Michael). Since you need both words, you need to place it (unindent it) so it's on the same indentation level as your for loop.


#7

Thanks :slight_smile: I got the answer


#8

Sure, no problem! :smiley:


#10

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