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 (2015) Shopping List App Shopping List Introduction

Paul Ryu
Paul Ryu
4,543 Points

Why does "DONE" print with the list when doing this problem within a function?

print("What do you need at Wal-Mart today?")
print("Type 'DONE' when you're finished.")
slist = []

def shopping_list():
  while(True):
    userinput = input(" >> ")
    slist.append(userinput)
    if userinput == "DONE":
      print ("We are done. Here is the list of things you need.")
      #del slist[-1]
      break
  for item in slist:
      print (item)

shopping_list()
In function result:
Water
Bananas
Pencils
DONE

Without function result:
Water
Bananas
Pencils

This is the code that I created before watching Kenneth Love making his own. When running it, I noticed that DONE prints with my list. Naturally, I looked back at some notes and used the del fx [-1] to fix it. However, I'm confused because in the video, Kenneth does not use Del. When I take my Del out, unlike him, my list prints out A, B, C, etc., DONE. So, I took out 'def shopping_list(): and shopping_list()' because I noticed he never did all this inside a function. I ran it again and it now prints out the list without "DONE". Why does it print "DONE" and require a Del with the list if it's in a function?

2 Answers

Bartosz PajÄ…k
Bartosz PajÄ…k
7,369 Points

If with break should be before appending so it breaks the loop when user types DONE without adding DONE to the list, then it wouldn't require to use del (as in Kenneth Love function).

Paul Ryu
Paul Ryu
4,543 Points

Ah, darn. Can't believe I missed something so simple. I typed it in the wrong order when re-doing the whole thing. Thank you very much.