Clearing the output data

I am making a clicker.
When using my program, there is a very long output.
I need my program to clear the old output and output a new message.

How can I clear the output?

Just in case, there is my code:

def shop():
  print('"l" для выхода')
  print('-------------Магазин------------')
  print('1. +1 за клик           10$')
  print('2. +5 за клик           50$')
  print('3. Х2 за клик           100$')
  print('4. +1 пассивный доход   80$')
  print('--------------------------------')

money = 0
print('Нажмите любую клавишу для заработка денег')
print('"q" для выхода, "s" для магазина')
while True:
  a = input()
  if a == "q":
    print(f'Итого: {money}$')
    break
  elif a == "s":
    shop()
  else:
    money += 1
  print(f'Баланс: {money}$')

On Windows, you can clear the console window from the Windows command line with cls, so you could run that from Python:

import os
os.system('cls')
1 Like

Here is an updated version of your program that clears the console before each new message. It introduces the clear_console function, which uses the os.system command to clear the console.

import os

def clear_console():
    # Clear console based on the operating system
    if os.name == 'nt':
        os.system('cls')  # For Windows
    else:
        os.system('clear')  # For Unix/Linux/Mac

def shop():
    print('"l" для выхода')
    print('-------------Магазин------------')
    print('1. +1 за клик           10$')
    print('2. +5 за клик           50$')
    print('3. Х2 за клик           100$')
    print('4. +1 пассивный доход   80$')
    print('--------------------------------')

money = 0
print('Нажмите любую клавишу для заработка денег')
print('"q" для выхода, "s" для магазина')

while True:
    a = input()
    clear_console()  # Clear the console before displaying new message
    if a == "q":
        print(f'Итого: {money}$')
        break
    elif a == "s":
        shop()
    else:
        money += 1
    print(f'Баланс: {money}$')
1 Like

@MRAB @Hichem
Thanks for the suggestion, but it doesn’t work, probably because I use Google Colab

I don’t understand what “clear the output” should mean in Colab. But I would suggest looking at the Colab documentation first.

There is never anything built in to Python for this, because Python is not responsible for actually making the text show up. It only sends data to the “standard output”, which is handled by the environment. On Colab, I guess that puts it into some text field of a web page, in your browser. When you run the code on your own computer, at the command line, the data goes to the terminal. If you use an IDE, the IDE has to put it in some graphical window (maybe it starts a separate terminal process for this).

The Python interpreter doesn’t know anything about where that text will go, therefore it can’t do anything about “clearing” it (or changing the position, etc.) In the terminal, we can use other programs like cls (on Windows) or clear (on Linux) that can make a system call, to configure the terminal. But every environment will have its own specific solution.

You should’ve mentioned that in the post.

A quick search found this:

How to Clear Python Output Programatically in Google Colaboratory? - Stack Overflow

which gives this answer:

from IPython.display import clear_output
clear_output()