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.