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 trialSamson Chapuramhuka
7,390 PointsEdit the code so that the for loop uses the enumerate method. Add a print statement above the existing print statement t
Python Sequences Challenge Task 2 of 2
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for current_element in rainbow:
print(current_element)
for index, current_element in enumerate(rainbow, 1):
print(f'{index}. {current_element}')
5 Answers
Josh Keenan
20,315 PointsThe challenge wants you to replace the first for loop for the one with enumerate and then add another print statement to output the item:
for index, current_element in enumerate(rainbow):
print(index)
print(current_element)
Immanuel wiessler
2,726 PointsGuys, I found the answer the way to solve the second question is pretty easy, here is the solution
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for index, current_element in enumerate(rainbow, 0):
print(f'{index}. {current_element}')
you just need to change the enumerate starting value to 0 instead of 1
Steven Parker
231,269 PointsThat gets the result, but it's not done the way the instructions asked for.
David Dunlop
2,844 Pointsworks like a champ Immanuel thanks
jon nikolakakis
1,639 PointsThis can work also.
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for index,color in enumerate(rainbow):
print(index)
print(color)
Steven Parker
231,269 PointsThe instructions say "Edit the code so that the for loop uses the enumerate method." So unlike most challenges, you will change the task 1 code instead of adding another loop.
The rest of the instructions say "Add a print statement above the existing print statement that prints the index of the current element." So the new print should print only the index. The other one will still print the value.
brandon supinski
Python Development Techdegree Graduate 27,983 PointsThis is how I got it to pass not sure if its intended or not Steven Parker can verify.
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] for item in enumerate(rainbow): print(f'index') print(item)