Home > Software design >  Does anyone know if it's possible to fix output to the top of the console after clearing?
Does anyone know if it's possible to fix output to the top of the console after clearing?

Time:07-07

I'm working on building a casino application and I want the user's login info (username & balance) to be fixed to the console even after clearing it. I have to clear the console because then there's going to be a bunch of inputs and prints that I don't want to be visible anymore. Not sure if the only way is to just clear the console and it reprint the information I want or if there is alternative ways. Below I've included an example of something I would like. Thank you!

Code:

balance = 100
user = 'username'   
print(f"Balance: {balance} User: {user}") # I want to keep this output constantly visible, even after clearing my console
# dummy output I want to eventually clear
for i in range(200):
    print('hi')
clearConsole()

Console:

Balance: 100 User: username

CodePudding user response:

the code you'd need for that is this:

import os

balance = 100
user = 'username'   
print(f"Balance: {balance} User: {user}")
"""
for i in range(200):
    print('hi')     not sure if you need this
"""

os.system('cls')

CodePudding user response:

Here you go! Use subprocess.

Assuming that you are using MacOS,

import subprocess

def clearConsole():
    subprocess.call("clear",shell=True)

balance = 100
user = 'username'
print(f"Balance: {balance} User: {user}")
for i in range(200):
    print('hi')
clearConsole()
print('this is a new line')

If you are using Windows, change the call function to

subprocess.call("cls",shell=True)
  • Related