16. Using strings in lists in functions


#1

Hi everybody,

Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned None, did you remember to return the result?

This is the latest error message that i keep getting, but i really cannot understand why. I've been back and forward with this and i'm returning the result ( i guess ). So where's the error? Can someone help me clarifying it to me?

Thanks a lot in advance,

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

print join_strings(n)

#2

append() is a method for appending to a list, not to a string. use + to join strings


#3

.append is used to join LIST not an EMPTY STRING...so use + this way
n = ["Michael", "Lieberman"]
def join_strings(words):
result =""
for item in range(len(words)):
result = result + words[item]
return result# Add your function here

print join_strings(n)


#4

.join() is used to join a list into a string, ,append() is used to append to a list


#5

So just to make sure that i understood it, the .append() does not work in this exercise cause it's like if i'm saying in the function to add to n the information that's already there and the .join() function is suppose to do the goal of the exercise and put what's inside n in between the quotation marks!!?

Meanwhile i've tried with the .join() function and still having an error:
Oops, try again. Your code looks a bit off. Check the console window for errors!

This is what's in the console:
Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in join_strings
AttributeError: 'list' object has no attribute 'join'


#6

this was adressed to csswhiz58601, for you, please don't use .join()

.append() doesn't work, because its not designed to add something to a string

use + to join the string

If you need more help, post an updated version of your code


#8

My bad, sorry about that...

Ok so i've given a little more thought about it and solved the problem in my code. However, it created a couple more questions about it. So the updated version is this (and that worked BTW):

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

print join_strings(n)

So, before anything, just to make sure that i'm having the correct line of idea, what i'm saying in the function with this code is that, i want the variable result to actually be the combination of the previous result plus the list inside any variable that i use as argument for the function, right?

Second, is it possible to make it simpler?

And last, could you also, if you can, clarify my previous question about .append() and .join()?

Sorry about the insistency but thanks for all the help until now


#9

you want results to contain a string, for all the elements in your list (words)

then here:

result = result + words[word]

you add to result what is already in result + a word from your list. You do this for all the words in the list

yep, don't use range()

i thought i covered .append()? You can always check the python docs.

.join() is actually a built in function to join a list into a string, so you could use this instead, but don't use to many built in function now, you won't learn much from it. I only mentioned .join() because csswhiz who used it


#10

I think i have it more clear now about the .append() and .join(), but i will check also the python docs.

So i've tried to simplify the code and remove the range() but i got an error, so does that mean that i also have to remove the len() to work or does it need any more changes to make it simpler?


#11

you also have to remove len(), for more i need to see an updated version of your code


#12

Still having an error message, but this time a different one. Here's an update:

n = ["Michael", "Lieberman"]

Add your function here

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

print join_strings(n)

Error:
Oops, try again. Your code looks a bit off. Check the console window for errors!

Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in join_strings
TypeError: 'list' object is not callable


#13

word contains items (not indexes) of words list


#14

Now it's working perfectly!!!

Quick question: Since you have much more experience, what version is more instinctively understandable for you? This last version or the previous working version of the code?

n = ["Michael", "Lieberman"]

Add your function here

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

print join_strings(n)


#15

the last one, there is no point of using range here


#16

Thanks a lot for all your help stetim94

I think that what helped to making a confusion was that in all the lessons there's, supposedly, always a continuity in the use of the code taught in the previous lesson but, apparently in this one was not the case.


#17

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