Issue
So I made a program in python that asks a user for input repetitively. After a while, the previous commands start to build up, as so.
> Ping
Pong!
> Hello
Hey there!
>say whats up?
Whats up?
I made the commands up just to show examples
I want to add an animation that adds a ... to the end of a word, such as
i choose.
then clear the line then
i choose..
then clear the line then
i choose...
and so on, but I need to clear the screen in order for this to work and I want the history of the users commands and responses to sill be there. Is there any way using python or importing os to only remove one line instead of the entire screen? Thanks!
Solution
You are looking for the carriage return character, \r
. When you print that character, the previous line will be cleared. For example:
import time
print('I choose',end='',flush=True)
time.sleep(1)
print('\rI choose.',end='',flush=True)
time.sleep(1)
print('\rI choose..',end='',flush=True)
time.sleep(1)
print('\rI choose...',end='',flush=True)
This is actually how people make the progress bar in command line.
Answered By - Mia Answer Checked By - Marilyn (WPSolving Volunteer)