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
R W
19,947 PointsLooking for feedback on code written for 'Extra Credit'
I'm working on the 'Python Collections Course' at the moment and have just finished the first section 'Lists Redux.' At the bottom of the section there is a set of instructions for 'extra credit' (which, as far as I know, does not actually get turned in anywhere). I have attempted Step 1 so far but my code feels sloppy and excessive so I'm looking for some feedback on it/hints or ideas on how to improve it.
"Take our shopping list script even further if you want. Some suggestions:
- Make it possible to move items from one position to another
- Change the formatting of the list display
- Create a CLEAR command that removes everything from the list"
Here is my code (please don't mind the comments too much, I write them to help me explain to myself what is doing what), it is the "shopping_list_extra_credit" script :
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsGood job! After running the code, I can see it working properly. The next steps would be to eliminate unnecessary print statements and add stronger checking of reasonable input values. Much of user input programming revolves around sanitizing input values and bulletproofing the code against erroneous values. Specific comments follow:
- when scanning to confirm an item is in the current list, it prints out 'Specified item is not in the list.' for every non-matching item. An alternative mechanism is to check for non-existence before the loop:
if to_move not in shopping_list:
print('Specified item is not in the list')
return
# Or you could simply try to remove it
try:
shopping_list.remove(to_move)
except ValueError:
print('Specified item is not in the list')
return
# Additional code hints:
# if needed, you can quickly find the position of something you know is present
index = shopping_list.index(to_move)
Other comments:
- after confirming item is in list, the number entered to confirm move target isn't verified, making the original MOVE target irrelevant. I typed in "a,b,c,d,e", "MOVE", "c", then confirmed with 4 and 2 to get "a,d,b,c,e"
- If a negative number is entered the add and MOVE functions behave unusually. "-1" inverts the list order on add.
- a non-number raises an unhandled exception.
- if the first move index given is greater than list length, an "index out of range" error is raised.
- The code doesn't check if an item is already on the list. This is OK for now, but if using
remove()it will remove the first encountered match which might not be the actual target. - The "-1" is needed because the user is working in positive integers (1, 2, 3, ...) while the list indexes use 0-based counting (0, 1, 2, ...)
Good luck on the other Extra Credit parts. Keep Going!
R W
19,947 PointsR W
19,947 PointsThank you so much for your thorough response and helpful tips on how to improve my code!