Using Strings in Lists in Functions


#1



Using Strings in Lists in Functions


Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned '' instead of 'xyza'


Replace this line with your code. 
words = ["Michael", "Lieberman"]
# Add your function here

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


#2

Be sure that return is not inside the loop. It should line up with for.


#4

it is still an error


#5

It's my nature to always start with the obvious and work from there. The above line is where your error is.

result.append(i)

Sometimes it is easier if we work with named variables so we can visualize what is happening.

for word in words:
    result.append(word)

#6

now it says that: Oops, try again. join_strings(['x', 'y', 'z', 'a']) resulted in an error: 'str' object has no attribute 'append'


#7

Now we are getting somewhere. Since we cannot append a string object (strings are immutable) how else could we do it? Hint: Think of i = i + 1 for example.


#8

4 posts were split to a new topic: Why result.append(words) wouldn't work


#12

2 posts were split to a new topic: Still getting the same error "Oops, try again


Still getting the same error "Oops, try again
#15

I'm not sure if this helps, but I sort of saw "append each word" as either a trick question or a misleading term. I ended up using the following code, which achieve the same result, using concatenation rather than the .append() function.

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

I figured it out when @mtf noted that strings are immutable and hence cannot be appended.


#16

A post was split to a new topic: How to get the list to combine with a string


#18

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