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 Second Shopping List App

My To Do List is not working

This is not part of my second shopping list app.. it's a to-do list. My objective is when I do

  • Apple
  • (Pretend: I type an entry in the todo list as a space by accident)
  • Banana

it usually shows up with the space as when printed

  • Apple
  • Space Shows Up
  • Banana

But, I want it to show up with the space eliminated

  • Apple
  • Banana

My Code here when run it will first work and once I type DONE, it reverts back to treehouse:~/workspace$ on a new line

todo_list = []
print("Welcome, this is a to-do list")
print("To finish type DONE")
while True:
  new_item = input("> ")
  if new_item == "DONE":
    break
  todo_list.append(new_item)
  if new_item:
    todo_list.remove(new_item)
for item in todo_list:
  print(item)

3 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya Ryan! I checked your code and instead of telling you hows it needs to be done , i'll point out the part of your code which is creating a bug in your code:

todo_list = []
print("Welcome, this is a to-do list")
print("To finish type DONE")
while True:
  new_item = input("> ")
  if new_item == "DONE":
    break
  todo_list.append(new_item)
------------------------------------------
  if new_item:                                   
    todo_list.remove(new_item)      
------------------------------------------
for item in todo_list:
  print(item)

Do you see that box i drew (well i tried lol)? That part of code says, if "new_item"(which is an input from user, right?) exists, then remove it from "todo_list" list. Also your last "for" loop is not indented with your infinite "for" loop. I hope this helped. (:

Thanks, So the reason i did if new_item: was because I was trying to say if the new item was a blank or false then it removes. am i supposed to add None?

Ari Misha
Ari Misha
19,323 Points

Hiya again! If you wanna do "if new_item is false or blank", try "if !new_item". Also if something doesnt exist, how could remove it from "todo_list", right?

!new_item does not work.

if !new_item:
    todo_list.remove(new_item)

I get

File "test.py", line 9                                                                 
    if !new_item:                                                                        
       ^                                                                                 
SyntaxError: invalid syntax    
Ari Misha
Ari Misha
19,323 Points

Hiya again! I think you misunderstood my point. I was saying that if you wanna check for if something is blank or false, you could try "if not new_item". But i also mentioned that if something doesnt exist, how could you remove it in the first place?

Btw im sorry i told ya to try "if !new_item" coz i forgot its Python. I assumed its Ruby lol. So yeah my bad. Try "if not new_item" (:

Hi, I just tried something else out. I'm not blaming you or anything (everyone makes mistakes), but the section between the ----- stays the same and does not need to be part of the while loop due to it happening after someone writes DONE, the loop break, etc... The only thing I needed to add was not Regardless, thanks for your help!

todo_list = []
print("Welcome, this is a to-do list")
print("To finish type DONE")
while True:
  new_item = input("> ")
  if not new_item == "DONE":
    break
  todo_list.append(new_item)
  if new_item:
    todo_list.remove(new_item)
---------------------
for item in todo_list:
  print(item)
---------------------