Using strings in lists and functions - Error: join_strings


#1



Using strings in lists and functions


This is the error message: Oops, try again. join__strings(['x', 'y', 'z', 'a']) returned ['', 'x', 'y', 'z', 'a'] instead of 'xyza'


I do not see what has gone wrong with my code to create ['', 'x', 'y', 'z', 'a'] instead of "xyza" like expected.
Any clarification would be appreciated.
Thank you in advance.

Code:

def join_strings(words):
    result = [""]
    for word in words:
        result.append(word)
    return result


#2

Your result should be String, not List of String (the brackets aren't needed)


#3

it wants you to add to a string, rather than an array, because an array is the result you're getting.
my suggestion is to make result an emptry string, "", and then add each word to result like this:

for word in words: 
    result += word

#4

The instructions say to use .append. I suppose that would work too?


#5

append shouldn't work for strings, only for arrays, let me look at the lesson again.
I checked, and the lesson does not say to use append.
It says Iterate through the words list and append each word to result., but append isn't formatted as a keyword like words and result are. In this case, append is just a verb that means add unto.


#6

Okay, thanks for the help - I got through!


#7

Hi,

Please help me to correct below code -

n = ["Michael", "Lieberman"]

def join_strings(words):
result=[""]
for item in words:
result=result+words[item]
return result
print join_strings(n)

==================
ERROR -

Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in join_strings
TypeError: list indices must be integers, not str


#8

Look at the first questions code from pyblaster10469, then read the answer from trigonometrical after that, the answer is there.


#9

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