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

How to create a list with input using commas - Python Shopping List

Hi, my original code is:

shopping_list = list()

print("What would you like to get at the store?")
print("Enter 'DONE' to stop adding new 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 are your items:")

for item in shopping_list:
  print(item)

I would like to create this list so that I can input:

Apples, Bananas, Cucumbers...

rather than enter it on a new line each time.

I have tried to figure it out but am unsure.

Part of the issue is how do you get it to stop and how do I get it to still read the number of items?

Other thing I would like to do is instead of it coming up as "> " to enter new items on the first one, how can I get it to start at 1. and somehow add +1 every time there is a new input?

Thanks!

1 Answer

Seth Reece
Seth Reece
32,867 Points

Hi Jake,

You probably want to use .split() and .strip() here. e.g.

shopping_list = list()

print("What would you like to get at the store?")
print("Enter 'DONE' to stop adding new items.")

while True: 
  new_item = input("> ")

  if new_item == 'DONE':
    break
  for item in new_item.split(','):
    shopping_list.append(item.strip())
  print("Added! List has {} items.".format(len(shopping_list)))
  continue

print("Here are your items:")

for item in shopping_list:
  print(item)

.split(',') slits on the comma, and .strip() removes the whitespace.