Using strings in lists in functions


#1


https://www.codecademy.com/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096

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

I don't understand how it turns those letters in numbers? I have specifically added str(word) in my code and before, when I create result, I used the "" to also define the string? I have tried some variations but I'm not seeing it


n = ["Michael", "Lieberman"]
# Add your function here

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

print join_strings(n)


#2

It's not. You're just counting up from 0. Look at the code and study where it's coming from, where is it meant to come from, did you mix something up? Name something differently from what it represents?

Isn't it odd that you should need to convert a word to string? What value type would a word be?


#3

it is odd that I would have to convert the word to a string.

when I do remove the str from it I am getting the following error

Traceback (most recent call last):
File "python", line 10, in
File "python", line 7, in join_strings
TypeError: cannot concatenate 'str' and 'int' objects

it's this error which made me go for str(word) although you are right and it doesn't make sense as it already would have been a string when we defined result = ""

to answer your question, "value type" of word, do you mean the type of variable? as that is a string. if it is something else, I don't know what you mean by that?


#4

Variables don't have types, they just refer to values.

If word was a string, then converting it to string would have no effect. If it has an effect, then it is not a string. Read that error message, it says what's up there. Error messages are for reading!


#5

I see what you are saying, and now I fully understand what the error message is referring to.

what I am not seeing though is how to change word to a string.

eventhough I think it's wrong I tried adding str( to word here

for str(word) in range(len(words)):

or even

for word in range(len(str(words))):

it doesn't make sense to me but I don't see how we can define word as a string?


#6

Why would you even try? If it's not a string, then clearly it's entirely the wrong value, there's nothing to change. Use a different value, ask yourself which value you should be using, where is that information in your function?


#7

Look, you loop through the results of calling range.
What does that function return? Not words. You are naming those things "word". They are not that.


#8

yes I fully realized there that in the loop no strings are returned. I just don't know how to turn word into a string. I am fully aware of the problem but I don't know how to change it. I think it'd be easier if you give me the solution there :slight_smile:


#9

There's no turning anything into a string to be done. You should just iterate through the words. They are strings. You're saying that you want to do something that doesn't make sense to do, so you have to reconsider what it is you want to do, which is not to turn anything into a string, but to use the strings that you already have.

You do not have any non-strings that represent words. So there is nothing to turn. Wrong action. Gotta re-consider what you need to do to go from the input to the output.

If you ask me for how to do something that can't be done, then the response is to do something else!


#10

I played around a bit more and I first came to

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

print join_strings(n)

then, I have to admit, I looked at other answers and saw that I had to change (word) to [word] which I understand as it has to be a list.

now, I have to say I am still confused about one thing:

you said earlier

"Look, you loop through the results of calling range.
What does that function return? Not words"

so I assumed that the loop returns integers and as the initial error message stated it looked that is the case.

but then you said:

"There's no turning anything into a string to be done. You should just iterate through the words. They are strings." which does confuse me as that makes it look like to me that the loop does return a string.

I'm sorry I'm slow here but I find it quite hard and would like to understand it fully!


#11

Looks to me like your confusion is, as has been the case the whole time, that you were storing numbers in a variable named word. There's a difference between that variable and the concept of a word. When I say iterate through words, I do not mean iterate through numbers. I mean iterate through the words that you were given as input, there are no other kinds of words involved, other than the numbers that you assigned to a variable called "word"

You create confusion by naming things differently from what they represent. If I have a bus then I'm not going to store that value in a variable named tree. That would be confusing. And if I actually meant to have a tree, then I would stop accessing a bus value, and instead access a tree. I wouldn't try to turn the bus into a tree, that is not something that I'm able to do

Also, note that rather than create a list of numbers to use as indexes, you should loop through the words. Otherwise you may as well create a list of numbers to iterate through a list of numbers to iterate through a list of numbers to iterate through a list of numbers to ..

do not do this:

stuff = ['baseball', 'kitten']
for index in range(len(stuff)):
    print stuff[index]

do this:

stuff = ['baseball', 'kitten']
for item in stuff:
    print item

What you're doing is:

stuff = ['baseball', 'kitten']
indexes = range(len(stuff))  # redundant
for index in indexes:
    print stuff[index]

You're creating a list so you can loop through a list. If you can loop through one list, then you can also loop through the other, so just do that instead of creating more lists


#12

I honestly don't know either. Just try your best and you'll do it. Trust yourself and you will find the way.


#13

What am I doing wrong here? Like the example itself says, "Don't add spaces between the joined strings!" It returned exactly what it asked to:

n = ["Michael", "Lieberman"]

Add your function here

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

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

Why does it say it should return "xyza"?!


#14

it should return a string, not a list


#15

Then you suggest I should change result = n[0] + n[1]?
I've already tried many different things, but I couldn't pass this exercise!


#16

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


#17

Thank you very much! It worked!


#18

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