16/18 Why Doesn't This Work?


#1

The Question:
Create a function that concatenates strings.

  1. Define a function called join_strings accepts an argument called words. It will be a list.
  2. Inside the function, create a variable called result and set it to "", an empty string.
  3. Iterate through the words list and append each word to result.
  4. Finally, return the result.

Don't add spaces between the joined strings!

My Code:
n = ["Michael", "Lieberman"]

def join_strings(words):
--->result = ""
--->for item in words:
------->result.append(item)
--->return result

print join_strings(n)

I get this error:
Traceback (most recent call last):
->File "python", line 10, in
->File "python", line 6, in join_strings
AttributeError: 'str' object has no attribute 'append'


#2

instead of doing

result.append(item)

you should do

result += item

The append() method does not exist for strings


#3

but I don't understand why using the .append() feature doesn't work


#4

The append() method does not exist for strings

oh ok thanks


#5

you can only append to a list but not a string


#6

yeah makes sense now, cheers


#7

This didnt work for me.
Code:

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


print join_strings(n)

This is the error message:
Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in join_strings
TypeError: bad operand type for unary +: 'str'


#8

Add this to your code,

Then run it again so you can debug it.


#9

Adding the print(type(item)) just printed type 'str' on the console. I didnt work. :frowning:


#10

Double check this,

Once you do you should get it quick, didn't notice it before.


#11

I changed my approached and started using the Method #2. It finally worked!!!

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


print join_strings(n)

This printed MichaelLieberman on the console.


#12

might be a little late, but your

result =+ item

was the wrong way, it should have been

result += item

the operator should come first


#13

Wow! That was definitely causing it not to work. Someone tried to tell me earlier and I didn't understand. Thank you.


#15

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

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

print join_strings(n)

Not sure where this is going wrong. I also tried result = result + words[i], but I received the same error.

Update: okay I figured it out I used return words ==> "Michael" + " Liebermann" but I needed to return result ==> MichaelLiebermann.

If anyone can provide a way to remember when to use which approach that would be great. In the last exercise where we had to concatenate integers it worked the other way.


#16

whats your new code?


#17

I ended up looking at other's examples and basically copied this:

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)

To understand how it works I made a hand not writing this in pseudo code. But honestly I am currently working though the Practice Makes Perfect section and so far I was not able to do them on my own, which is frustrating. I am not sure if everyone has the same problems or if I should start over because I failed to understand some of the basic things.


#18

you do not need 'print' in
'print join_strings(n)'
and you need to indent it properly.


#19

This did not work for me :frowning:


#20

This definetly works

n = ["Michael", "Lieberman"]

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

print join_strings(n)