clear the screen

Y

Yuanyuan Li

How to clear the screen? For example, in the two player game. One player sets a number and the second player guesses the number. When the first playerenters the number, it should be cleared so that the second number is not able to see it. My question is how to clear the number.
Thank you!
 
D

Dave Angel

How to clear the screen? For example, in the two player game. One player sets a number and the second player guesses the number. When the first player enters the number, it should be cleared so that the second number is not able to see it. My question is how to clear the number.
Thank you!

Welcome to the python mailing list.

The first thing you'd have to specify is your environment. Are you
writing a gui program, and if so, with which toolkit (tk, wx, etc.).

Or are you writing a console program? The simplest portable way is to
issue 50 or so newlines, so things scroll beyond easy visibility. Of
course, many terminal programs can easily scroll back, perhaps by using
the mouse wheel.

If you want something better than that, you'd have to specify your
Python version and Operating System. Then somebody familiar with the
quirks of that particular system can chime in.
 
S

Steven D'Aprano

How to clear the screen? For example, in the two player game. One player
sets a number and the second player guesses the number. When the first
player enters the number, it should be cleared so that the second number
is not able to see it. My question is how to clear the number. Thank
you!


What sort of screen? A GUI window, or in a terminal?

If you have a GUI window, you will need to read the documentation for
your GUI toolkit.

If it is in a terminal, that will depend on the terminal. The best
solution is to use Curses, if you can, but that is Unix only.

Another solution is this:

print("\x1b[H\x1b[2J")

although that only works in some terminals, and it will not work in IDLE.


Another solution is this:

import os
result = os.system("clear") # Linux, Mac, Unix

or for Windows:

result = os.system("cls")


but again, it may not work in IDLE.


Last but not least, if everything else fails, try printing a whole lot of
blank lines:

print("\n"*100)


ought to do it on all but the tallest terminals, and amazingly, it even
works in IDLE!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top