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
chailatte
941 PointsDid I use the enumerate function correctly?
At the end of the 'Shopping List Take Three' video, Kenneth suggests finding a way to use the enumerate function instead of the indexes he uses in the video. My program here seems to work OK but just wanted someone to check it for me. I wasn't sure if that 'value' portion is part of the enumerate function.
import os
shopping_list = []
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def show_help():
clear_screen()
print("What should we pick up at the store?")
print("""
Enter 'DONE' to stop adding items.
Enter 'SHOW' to show current items.
Enter 'HELP' to see a help message about special commands.
""")
def add_to_list(item):
show_list()
if len(shopping_list):
position = input("where should I add {}?\n"
"Press ENTER to add to the end of the list\n"
"> ".format(item))
else:
position = 0
try:
position = abs(int(position))
except ValueError:
position = None
if position is not None:
shopping_list.insert(position-1, item)
else:
shopping_list.append(new_item)
show_list()
def show_list():
clear_screen()
print("Here's your list:")
for item, value in enumerate(shopping_list, 1):
print(item, value)
print("-"*10)
show_help()
while True:
new_item = input("> ")
if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
break
elif new_item.upper() == 'SHOW':
show_list()
continue
elif new_item.upper() == 'HELP':
show_help()
continue
else:
add_to_list(new_item)
show_list()
1 Answer
Jeff Muday
Treehouse Moderator 28,732 PointsNice work... you are very close! The enumerate method has been around since Python 2.3 but it is not always taught in introductory lessons.
https://docs.python.org/2.3/whatsnew/section-enumerate.html
In your code, if you reverse the order of value and item you will get it.
I am going to use INDEX rather than value, since I think index is a little more straightforward example of how it works.
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> my_list = ['eggs','bread','cheese','milk']
>>>
>>> # without enumerate, you need to have a separate index variable and increment statement
... index = 1
>>> for item in my_list:
... print(index, item)
... index +=1
...
1 eggs
2 bread
3 cheese
4 milk
>>> # with enumerate, you save two lines of code and your code is arguably better
... for index, item in enumerate(my_list, 1):
... print(index, item)
...
1 eggs
2 bread
3 cheese
4 milk
chailatte
941 Pointschailatte
941 PointsGreat! Thanks for the explanation!