0

I'm taking the codeacademy course and it asks me the following:

Define a function called join_strings accepts an argument called words. It will be a list. Inside the function, create a variable called result and set it to "", an empty string. Iterate through the words list and append each word to result. Finally, return the result. Don't add spaces between the joined strings!

So what I did is:

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

But the console prints me:

Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned ['x', 'y', 'z', 'a'] instead of 'xyza'
2
  • No, you didn't do that, that code would not return anything but crash on the return. Commented Mar 16, 2015 at 6:07
  • Misspelling corrected Commented Mar 16, 2015 at 6:43

4 Answers 4

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

you have missed this result += words concatenation part and return result

you have to use this

for i in words:
            result += i

Update

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

g= join_strings(['x', 'y', 'z', 'a'])

print(g)

o/p : xyza

From your post :

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

g= join_strings(['x', 'y', 'z', 'a'])

print(g)

You have to use the index value of the word. Since you are getting the range and iterating you have to specify the index result += words[i]

Sign up to request clarification or add additional context in comments.

1 Comment

Those who downvoted please tell me the reason. I am getting the proper o/p. why did you downvoted then ?
1

Try this one:

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

Basically, we have an empty string. We can add more to this string by using the += operator. This operator is short for saying x = x + y.

1 Comment

Get rid of the unnecessary semi-colons.
0

Try the following code:

from operator import add
def join_strings(words):
    return reduce(add, words)

2 Comments

This does not satisfy the assignment, which is, essentially, to write the join function
Code only answers are generally discouraged unless they answer the question in full and are "self documenting" (contentious term). This answer doesn't follow the method outlined in the brief and isn't self documenting, so I don't think it fulfils those criteria.
-1

The following has worked for me:

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)

1 Comment

Please avoid posting an answer wich is identical to another previously posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.