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

While TRUE

Hi Everyone!

I found the construction "While True + an if condition, and than break" somehow strange. My solution would be:

my_list = []
new_item = input("What should we pick up at the store? ")
while new_item != "DONE":
    my_list.append(new_item)
    new_item = input("Something else? ")
if new_item == "DONE":
    print("Here's your list.")
print(my_list)

Is it a good approach? If not, why?

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Great job exploring new approaches. Both your approach and the one in the video are valid. Which to use depends on the task. For straight-forward conditions your simpler approach is clear. As the exit condition becomes more complicated or several different exit conditions are needed then the video approach works better.

A hybrid of the two approaches using a variable instead of True can also be used then anywhere in the while loop, the variable can be set to False to mark the last iteration or a break can be used.

Your last if statement is redundant since new_item must be "DONE" to reach that point.

Thank you for the quick answer!

Roberto Núñez
Roberto Núñez
4,256 Points

Hi Krisztina!

Actually I did a similar idea as you the first time and showed to a friend. He knows programming pretty well and gave me a very interesting answer. He actually said that "code should look sophisticated", and that includes not having to do the same validation or declaration more than once (his words).

I didn't think it was relevant in most cases but after some extra runs of the exercise I at least realized his advice can save some code lines. For example in your code it declares two times new_item and uses two validations for the DONE part. Pretty clever I think but just wanted to share with you that comment I received.

May I also recommend a simple solution. One of the issues with this solution given in the video is that you should refrain from using strings as much as possible. A better way to print this shopping_list script is to turn the item into a variable = new_item. That way it makes the item variable more relevant to the short app.

#make a list to hold onto our items
shopping_list = []
# print out instructions on how to use the app
print ("What should we pick up at the store?")
print ("Enter 'DONE' to stop adding items.")

while True: 
  #ask for new items
  new_item= input("> ")
  item = new_item

  # be able to quit the app
  if new_item == 'DONE':
    break

# add new items to our list
  shopping_list.append(new_item)

# print out the list
print("Here is your list:")

for item in shopping_list:
  print(item)