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?
Asked
Active
Viewed 703 times
0
-
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 Answers
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
-
I doubt this will will for multiline print statements as-is, but I think is possible. – anon01 Nov 15 '20 at 19:17
-
Hmmm this doesn't seem to work for me. It just waits 2 seconds, then prints out "new text!". – Aiden Chow Nov 15 '20 at 19:18
-
I don't know why that would be. What system are you on? Are you running the code from file or interpreter? – anon01 Nov 15 '20 at 19:21
-
Ok if I replace `print(text, end="\r")` with `print(text, end="")` then it works. I don't know why this would be the case though. – Aiden Chow Nov 15 '20 at 19:23
-
`"\r"` may be interpreted differently on different systems. Are you on windows by chance? – anon01 Nov 15 '20 at 19:24
-
-
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