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 Collections (2016, retired 2019) Lists Shopping List Take Three

Hussein Amr
Hussein Amr
2,461 Points

Can someone please explain why my clear function isn't working

import os


mylist = []

def clear_screen():
  os.system = ("cls" if os.name == "nt" else "clear")  


def help():
    #print out instructions 
    print("type the stuff you want to add to the list")
    print("""
    type in 'DONE' when you are..
    Enter 'HELP' when you need it
    Enter 'SHOW' to show the added stuff""")

def add_to_list(new_item):
    if len(mylist):
      position = input("where should i add {} ?\n"
                       "press ENTER to just ass it to the end of the list\n"
                       "> ".format(new_item))
    else:
      position = 0

    try:
      position = abs(int(position))
    except ValueError:
      position = None
    if position is not None:
      mylist.insert(position-1,new_item)
    else:
      mylist.append(new_item)

    show_list()  

def show_list():
    clear_screen()

    print("Here's everything on the list : ")

    index = 1
    for items in mylist :
        print("{}. {}".format(index, items))
        index += 1

    print("-" * 15)

help()

while True:
    new_item = input("> ")

    #have an exit button
    if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
        break

    elif new_item.upper() == 'HELP' : 
        help()
        continue

    elif new_item.upper() == 'SHOW':
        show_list()
        continue
    else:       
        add_to_list(new_item)


show_list()

3 Answers

Steven Parker
Steven Parker
231,275 Points

Looks like you may have a stray symbol.

I suspect you don't want that equal-sign between "system" and the open parenthesis.

Hussein Amr
Hussein Amr
2,461 Points

it worked :D thanks

Daniel Maia
Daniel Maia
6,228 Points

When I use clear_screen() the function works but not on PyCharm on the run window. Is there a separate function for this?

I have been using print(" "*100) for now

Steven Parker
Steven Parker
231,275 Points

That clears the entire screen? I would have expected just a blank line. But this should do it (adjust value for the number of lines on your screen):

print("\n" * 80)

I'm not a PyCharm user, but perhaps someone who is can suggest a better solution.

Also, your question might be seen by more students if you create a new one. Comments added to an old question are only likely to be seen by the person who originally posted it and anyone who answered already.

Daniel Maia
Daniel Maia
6,228 Points

Ok cool thank you :)