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

Chaya Feigelstock
Chaya Feigelstock
2,429 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 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
2,429 Points

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