1
office_list = []
print("Type in your office supplies.\nEnter 'DONE' to print out your list.\n--------------------\n")

while True:
  list = input("> ")  
  if list == 'DONE':
    break
  office_list.append(list)

print("Here is your list\n--------------------\n")

for ls in office_list:
  print(ls)

I've been trying to find this online but seem to have trouble trying to find the correct vocabulary I believe.

What I am trying to make the program do is clear what I have written to make the list and then print the list. What happens in the program right now is it will have the words I typed on top of the list and print when I enter the word 'DONE'.

  • Possible duplicate of [How to clear the interpreter console?](https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console) – JakeD Jun 09 '17 at 19:09
  • Perhaps it's better to run `clear && python foo.py` – salezica Jun 09 '17 at 19:27
  • @slezica I need to clear text while a program is run. Not when I'm trying to start the program. –  Jun 09 '17 at 19:29

3 Answers3

2

You can use the os module. Under *nix, you can use os.system('clear') or os.system('cls') under Windows.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • 1
    Additionally, you can programmatically determine what OS you are running with `sys.platform`. Then you can decide if you should use `cls` o `clear` – DarkCygnus Jun 09 '17 at 18:56
  • @GrayCygnus Better to use `os.name`: https://stackoverflow.com/a/18476686/6655092 – JakeD Jun 09 '17 at 18:56
  • I get a syntaxError. Would using work spaces in team treehouse be the problem? –  Jun 09 '17 at 18:59
  • @pycoder not necessarily better, there are several alternatives, see [this](https://stackoverflow.com/questions/1854/python-what-os-am-i-running-on) – DarkCygnus Jun 09 '17 at 19:00
  • @GrayCygnus It doesn't matter if it is Linux or Mac OS X, just whether it is Unix or Windows. – JakeD Jun 09 '17 at 19:04
  • @RussellAlson no, a `SyntaxError` means you are not writing valid Python. – timgeb Jun 09 '17 at 19:12
  • @RussellAlson in case you were being sarcastic, I can't offer any more explanation without seeing the full error message. – timgeb Jun 09 '17 at 19:18
  • 1
    @timgeb In no way was it used as sarcasm. It's honestly genuine. Still in a newby level right now and all the small details are nice to take in. –  Jun 09 '17 at 19:21
1

Using the os module, you can run shell commands. To clear the console on Linux/macOS you can use the clear command, on Windows there's cls:

import os
import sys

def clear():
    if sys.platform == 'windows':
        os.system('cls')
    else:
        os.system('clear')
salezica
  • 74,081
  • 25
  • 105
  • 166
enedil
  • 1,605
  • 4
  • 18
  • 34
  • There is a systaxError: invalid syntax. Would this be because Team Treehouse doesn't support this? –  Jun 09 '17 at 18:56
  • try this @RussellAlson – enedil Jun 09 '17 at 18:59
  • It didn't work in team treehouse. It just passes by the def clear() function or it just didn't output anything. @enedil –  Jun 09 '17 at 19:03
0

Just print enough newline characters like this:

print('\n' * 50)

It doesn't hurt to print out too many lines for a console application as this will all happen in a split second. This method is cross-platform and should work in nearly any environment.

The OS-level answers actually do the same thing, but the OS knows exactly how many lines to print. If you don't care about hiding the precise number of lines that are shown on the screen, just print out enough (within reason) to clear the console.

JakeD
  • 2,788
  • 2
  • 20
  • 29
  • Ah I see what you mean. I'm still having problems with using the OS-level answers atm. I might as well use print('\n' * 50) for now. I'll have to learn the OS commands one of these days. –  Jun 09 '17 at 19:18