Using an index to access items in a list of strings


#1

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)

What is wrong here? This gives an error and I don’t understand why? the solution to the exercise gives doesn’t’t use the range method so doesn’t help with the answer…?


What do I add to result if using the range( ) method?
#2

Hi @grogle ,

words is not a function, but you are using the syntax for a function call to attempt to access it here …

    result += words(i)

Use square brackets to apply an index to a list.


#3

Thanks,
I had meant with the square brackets…

The console output is now correct but I have an error message telling me my code doesn’t look right? is this a bug in the exercise?


#4

Please post the most recent version of your code, so we can test it. Be sure to format the code so that we can view and copy its indentation. For advice on formatting posted code, see How to ask good questions (and get good answers).

Also provide us with a link to the exercise.


#5

ok the link to the exercise is here:
https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/using-a-list-of-lists-in-a-function?action=resume_content_item

and teh code is here:

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)

Although now when I run it it seems to be OK! typical… maybe I hadn’t cleared the error…


#6

Thanks for the link and the formatted code. However, the link went to a different exercise. The exercise you are doing is here: Using strings in lists in functions.

Glad you got it working. :smile: