Python Introducing Lists Using Lists Continental

Chaya Feigelstock
Chaya Feigelstock
1,945 Points

what is wrong here? for continent in continents[0,3,5,6]: print("*", continent)

getting "ERROR: test_code_untouched.."

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]

for continent in continents[0,3,5,6]:
    print("*", continent)

1 Answer

Alex Koumparos
MOD
Alex Koumparos
Python Web Development Treehouse Moderator 32,018 Points

Hi Chaya,

The ERROR: test_code_untouched.. means that the challenge's unit test called test_code_untouched is failing.

Let's look at what that unit test tells us (the right side of the screen):

Traceback (most recent call last):
  File "", line 37, in setUp
  File "/workdir/utils/challenge.py", line 24, in execute_source
    exec(src)
  File "", line 11, in 
TypeError: list indices must be integers or slices, not tuple

The last line of the Traceback is the actual error: TypeError: list indices must be integers or slices, not tuple

In this case it is referring to the list you are iterating through in this line:

for continent in continents[0,3,5,6]:

When you use brackets after a list, you are accessing an index (e.g., continents[0]) or a slice (e.g., continents[2:4]). You can't use the brackets to do what you are trying to do, which is a sequence of indices.

It would be valid syntax, although tiresome to type, to achieve what you are trying to do like this:

for continent in [continents[0], continents[3], continents[5], continents[6]]:

This change will pass the unit test (since the syntax is valid) and the challenge. However, it does violate the intent of the challenge, which is to get you to write code that will programmatically check for values starting with A and only printing those (hence the hint:

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

). Something like the following:

  • loop through all the continents (exactly like you did in the first challenge);
  • check if the continent starts with (i.e., the character at index 0) 'A';
  • in the case where the condition is true, print '* ' then the continent name;
  • otherwise don't print anything;

Cheers,

Alex

Chaya Feigelstock
Chaya Feigelstock
1,945 Points

THANK YOU!!! This was so incredibly helpful and easy to understand. You are awesome,