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

Tom de Visser
Tom de Visser
1,083 Points

Different way

I did it like this, it works. Is it okay?

to_do_list = []

new_item = None

while new_item != "DONE":
  if new_item != None:
    to_do_list.append(new_item)
  new_item = input()
else:
  print(to_do_list)

[MOD: added ```python formatting -cf]

1 Answer

dublinruncommutr
dublinruncommutr
5,944 Points

I like it - It runs fine but I suspect it's bad form to allow an if/else statement to flow outside of the containing block (WHILE block in this situation).

You can simply remove the outer 'else:' and unindent the print(to_do_list) statement:

to_do_list = []

new_item = None

while new_item != "DONE":
  if new_item:
    to_do_list.append(new_item)
  new_item = input()

print(to_do_list)

I've also shortened if new_item != None as this can be treated as a slightly more readable True/False comparison where we are simply checking for a non-empty (True) new_item.