16/18 Using strings in lists in functions


#1

Guys, can somebody help me?

I got stuck in this:

n = ["Michael", "Lieberman"]
def join_strings(words):
[TAB]result = ""
[TAB]for i in range (len(words)):
[TAB][TAB]result += words[i]
[TAB][TAB]return words
print join_strings(n)

The problem is the result, because it actually works (well, at least apparently), but this message appears: "Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned ['x', 'y', 'z', 'a'] instead of 'xyza' "

Why? and how can I solve it?


#2

To paste your code with indents use three backquotes/backticks before and after your code.

About your code, try with return with only one TAB

However there is still something wrong. Think about what do you want to return from this function


#3

You need to:
return result

not return words as in your code, and make sure it's outside the for loop ie. one tab not two.
Hope that helps!


#4

don't use append. it's don't support for string
i hope it help
n = ["Michael", "Lieberman"]

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


#5

Hi!

I was having the same problem. First of all, don't use the append operation (the directions are a little misleading). Second of all, don't put quote marks around the i (or what you used in your for loop/iterating over the list). Here's what my code looked like (I used a for loop to iterate over the list instead of using the range operation):

n = ["Michael", "Lieberman"]

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

print join_strings(n)

Hope this helps!


#6

why i cant use result+i, just result = result + i ?


#7

This is my code and it worked with += :grinning::ok_hand: :

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in range(len(words)):
result += words[i]
return result

print join_strings(n)


#8

because you have to have result = something to return it. also, you can use += to say " x = x + y" in this case, "result += i" would say "result = result + i"