Using an index to access items in a list of strings

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…?

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.

1 Like

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?

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.

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…

1 Like

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: