16. Using Strings in lists in functions


#1



Why its not working ??

These are the errors:
Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in join_strings
AttributeError: 'str' object has no attribute 'append'


Replace this line with your code. 

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

print join_strings(n)


#2

Hi there!
Do you remember that you can only use .append when you want to add a string/digit only to a List and not to a string?

I suggest adding each letter to the resultevery time the program iterates over it.

Hope this helps out with your confusion.
Happy Coding :slight_smile:


#3

Hi :smiley:
Thanks for responding !
Is this what you meant ? Cause errors are stll there

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)
Thank you for you time !! :smiley:


#4

Not exactly.
Remove the range in your loop, there's no point to that.

And, what I mean by adding each letter to result is this.

result = result + i

Hope this helps you out :wink:


#5

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