0

I'm a beginner coder and I'm using python to create a hangman game. I already have written the code for the game but I wanted to have string that is outputted for the user to read that disappears when they have to work out the word. Is there a way to do this or am I overstretching python's boundaries?

am8ture
  • 21
  • 7
  • what are you using for output? just printing to console? – anon01 Nov 15 '20 at 19:01
  • Its doable in python... but there are a hundred different choices. You could use `tkinter` built into python to make a GUI. You could use `curses` on a terminal. You could use Windows console API or cook up the terminal escape codes yourself. You could just write the string without newline, use the backspace character and write blank lines. But that makes it a difficult question for SO where we avoid large design issues like this. – tdelaney Nov 15 '20 at 19:03
  • @anon01 yes i am – am8ture Nov 15 '20 at 19:03
  • example code would be helpful to interpret your question – anon01 Nov 15 '20 at 19:07

2 Answers2

2

Here is a solution where text disappears after a moment and is replaced by text on the same line. A few notes:

  • the second print statement is used to overwrite the first with empty characters
  • this method only works for single line statements
  • \r behavior may be platform dependent

code:

import time

text = "some text!"
print(text, end="\r")
time.sleep(1)
print(" " * len(text), end="\r")
time.sleep(1)
print("new text!")
anon01
  • 10,618
  • 8
  • 35
  • 58
0

If you want to clear the whole terminal window:

from os import system, name 
import time
   
def clsScreen():  
    if name == 'nt':  # for Windows OS
        v = system('cls') 
    else: # for Linux/UNIX
        v = system('clear') 

print('Sting appeared to the user') 
sleep(1)
clsScreen()

In case you want to clear a part of the output then that is not possible unless you create a dedicated display window with pygame(maybe)

WinterSoldier
  • 393
  • 4
  • 15