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
Sean Loke
14,659 PointsPython Collections - Lists - Extra Credit - Thoughts on my code
Extra Credit 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
Please provide some thoughts on my code !!!!!!!
def move_item():
show_list()
while True:
if len(shopping_list) > 1:
chosen = input('Which item would you like to be moved\n')
if chosen.lower() in shopping_list:
try:
location = abs(int(input('Where would you like to move {} to (Provide a
number)?\n'.format(chosen.lower()))))
except ValueError:
location = None
if location is not None:
shopping_list.remove(chosen.lower())
shopping_list.insert(location - 1, chosen.lower())
show_list()
break
else:
shopping_list.append(chosen.lower())
show_list()
break
else:
print('Make sure to enter a valid item name')
continue
else:
print('Please add an additional item!')
break
main()
def remove_everything():
if len(shopping_list) < 1:
print('Please add at least one item!')
main()
else:
shopping_list.clear()
show_list()
1 Answer
andren
28,558 PointsThere is a bit of a logic bug in your move_item function. If you specify a valid entry but do not provide a position to move the entry then you add that it to the end of the list, but you don't remove the entry that was being moved.
Which means that the entry ends up being duplicated instead of being moved.
Like this:
> move
Here's your list:
1. apple
2. milk
----------
Which item would you like to be moved
apple
Where would you like to move apple to (Provide a number)?
Here's your list:
1. apple
2. milk
3. apple
----------
>
Other than that issue your code looks quite good, there are lots of input checks and appropriate warning messages, which I like.
Sean Loke
14,659 PointsSean Loke
14,659 PointsChanged the else statement. Is this okay?
andren
28,558 Pointsandren
28,558 PointsYes that works fine, and is a bit more consistent with the rest of your function in terms of providing a warning when invalid input is entered.