Python Introducing Lists Using Lists Continental

Kaden Currier
Kaden Currier
1,312 Points

I didn't alter the list???????

it doesn't work

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
print("continents:")
for continents in continents:
    print(" * " + continents)

2 Answers

Steven Parker
Steven Parker
176,584 Points

When you use the list as the loop variable ("for continents in continents:"), the loop will alter the list as it runs.

To prevent this, give the loop variable a different name.

Kaden Currier
Kaden Currier
1,312 Points

Do you mind giving me an example I don't quite understand?

Steven Parker
Steven Parker
176,584 Points

Instead of "for continents in continents:" you might write "for item in continents:".

When the list name is a plural word, it's also popular to use the singular version (without the "s") as the loop variable:

for continent in continents:
Kaden Currier
Kaden Currier
1,312 Points

when I do that it says that it needs str not list

Steven Parker
Steven Parker
176,584 Points

Maybe you forgot to change the "print" statement to use the new variable name?