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 Basics (Retired) Shopping List Shopping List Project

Adding commas to the shopping list items

import sys
shopping_list = list()

print("What should we pick up at the store? ")
print("Enter 'DONE' to stop adding items.")

while True:
    new_item = input("> " )

    if new_item == 'DONE':
        break

    shopping_list.append(new_item)
    print("Added! List has {} items.".format(len(shopping_list)))
    continue


print("Here's your list: ")
#[print(item) for item in shopping_list]

for i, k in enumerate(shopping_list):
    if i == len(shopping_list) - 1:
        print(k)
    else:
        sys.stdout.write(k + ', ')

For edification, also critique my code if it's too verbose in places! Is this pythonic?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Seems nice although I don't think I'm using for and if statements to print out shopping_list item. But since you probably got that from watching video it's totally cool to try out. Also, do you need that continue at the end of while loop?

1 Answer

Hi ogechi1

your code has nice spacing and indentation, seems pythonic to me. Also I dot think you should keep showing the user the list count every time they add something ( but again that's your call ). In terms of achieving the objective you can use the string.join function to accomplish this which saves writing 5 or 6 lines of code. And as programmers less code more work is the way to go.

shopping_list = []

print("What should we pick up at the store? ")
print("Enter 'DONE' to stop adding items.")

while True:
    new_item = input("> " )

    if new_item == 'DONE':
        print("Your! List has {} items.".format(len(shopping_list)))
        break

    shopping_list.append(new_item)
    continue


print("Here's your list: ")
#[print(item) for item in shopping_list]

print(",".join(shopping_list))

hope this helps

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Yeah I prefer to use join for that nice catch.

That's super helpful, thanks so much Andreas!