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 Python Sequences Sequence Iteration Iterating With For Loops

I don't know where I am going wrong

This is for challenge task part 2 and it's saying that index is not defined

iterating_lists.py
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for colours in enumerate(rainbow, 1):
    print(f'{index}. {colours}')
    print(colours)
Moaz Mansour
Moaz Mansour
4,594 Points

Hi Falah,

The code is failing because the variable index has not been assigned before called in

print(f'{index}. {colours}')

try changing your for statement to include the index variable like this

for index, colours in enumerate(rainbow, 1):

This should work

1 Answer

Scott Bailey
Scott Bailey
13,190 Points

You need to update your for loop when using the enumerate method - you have a counter as well as a value to include.

This is a link that should help explain it better than I can!

I've put my code below to help out!

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

for counter, color in enumerate(rainbow):
    print(f"{counter}")
    print(f"{color}")