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 Collections (Retired) Lists Redux Shopping List Take Three

Srikanth Srinivas
Srikanth Srinivas
1,465 Points

This sort of shopping list would still do the same things as yours when i compile it?

shopping_list = []

print("This is a shopping list.")
itemtoadd = input("Enter an item you wish to add, or type DONE to exit.")

while itemtoadd != "DONE":

    index = int(input("What number would you like to place this item in your list?"))
    shopping_list.insert((index)-1,itemtoadd) 
    print("You have entered {}. Your list now contains {} items.".format(itemtoadd,(len(shopping_list))))
    itemtoadd = input("Enter an item you wish to add, or type DONE to exit.")

if itemtoadd == "DONE":
    print("Your list now contains the following items: {}".format(",".join(shopping_list)))

I wrote this on an online compiler and it seems to work the same way yours does, but with reduced lines of code. Am I missing something? Thanks!

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It'll accomplish the same goals, more or less, but doesn't take into account the fact that people might enter non-ints when you ask for an index or might not want to put the new item somewhere special.

You don't need that last if, either. Once the while loop ends, the "DONE" is implied and that code should run.

Good job coming up with another solution!