2

I'm not sure if this is a dumb question or not, but I can't really find any information regarding it. I would like to know if and how to remove previous print statements from the program as it is running. This is kind of hard to explain! Here is some example code:

while True:
    x = input('Where would you like to go? ')
    if x == 'home':
        print('You are at home.')
    elif x == 'field':
        print('You are at the field.')
    elif x == 'exit':
        break

And this code isn't really important. Just an example. When the code runs we get the question: 'Where would you like to go?' And we reply, 'home'. Then the print statement prints 'You are at home.' and the loop iterates again with the original question: 'Where would you like to go?'.

All that text is now in the window it runs in and as I keep doing things with the program, it builds up and I have a ton of text above where I am typing at the bottom of the window.

Is there a way to have this program run the question, me input say, 'home', and for it to run the code then remove itself so that the entire time the program is running I am typing my inputs at the top of the screen without the clutter of all the previous inputs and outputs? If this question can be asked a better way I can edit this.

ShroomBandit
  • 441
  • 1
  • 5
  • 12

3 Answers3

1

You need to clear the terminal each time. From clear terminal in python

import os
os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
Community
  • 1
  • 1
Steve
  • 7,171
  • 2
  • 30
  • 52
1

This depends where the script is running. If it is running in a terminal, you can utilize os.system to clear the terminal:

(Linux): os.system('clear')

(Windows): os.system('CLS')

If you're running in IDLE, then the best you can do is write a clear() function which will print a number of newlines. There is no way (that I know of) to actually clear the IDLE console.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
1

If you don't want to use Steve's list/bool hack -

os.system('cls' if os.name == 'nt' else 'clear')
Alex L
  • 8,748
  • 5
  • 49
  • 75