Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Continental

How would you solve this challenge using the .index method?

I have solved this challenge pretty easily, however the question reads:

HINT: Remeber that you can use the .index method

I cannot for the life of me figure out how to solve this challenge by using that method. I must not be understanding the method correctly. Could someone show me how you would solve it by using .index to better improve my understanding?

ps: I butchered the quote but you get the point

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great, but you misread/misquoted the hint. Here's the actual hint shown in the challenge:

HINT: Remember that you can access characters in a string by index

This says nothing of using the index method, which is only available on lists and not strings as covered in this Python documentation. The hint here was meant to remind you that you can access the individual characters in a string through subscripting just like you would access an element in a list, which you obviously did just splendidly!

The index method was not meant to be used here. If you absolutely wanted to use it there are a number of factors here. First, you will have to change the string to a list to use the index method. You will also have to account for any errors that happen for lists that do not contain an "A". And finally, you will have to convert that list back to a string.

Here was my solution that used the index method:

for continent in continents:
    # convert the string to a list
    continent = list(continent)
    try:     # attempt this
      # if the first capital A is found at index of 0
      if continent.index("A") == 0:
        # join the list back together to make a string
        continent = "".join(continent)
        # print the string
        print("*", continent)
    # if the list does not contain a "A"
    except ValueError:
      # continue to next iteration

This, of course, goes way beyond the scope of the current course you're taking, but I hope it helps! :sparkles:

I totally misinterpreted that hint! I understand now, thank you!