Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide something that you have already printed in Python [duplicate]

Tags:

python

I am currently making a memory game where I print a few words and after a given time the words are shuffled and then one word is removed and replaced with a new one. Then I would get the user to answer which word has been removed and which word has then replaced that word. For instance, it will print:

CAT DOG MOUSE HORSE

And after 10 seconds, I would like those words to be hidden, be shuffled and replace one word with a new word so that it prints, for example:

DOG HORSE RABBIT CAT

I understand that I can use time.sleep() for the program to suspend the execution of any other code.

Would it be easier to essentially "unprint" the first set of words and then print the new one OR replace the first printed set of words with the new one.

like image 691
Kye Avatar asked Mar 22 '15 16:03

Kye


People also ask

How do you hide something in Python?

In Python, data hiding is achieved using a feature of double underscore (__) as a prefix. This initiates the hiding feature in the attribute. As the attribute becomes inaccessible for the users.

How do you change already printed text in Python?

Here's a tip: '\r' erases what has been printed on the current line, and put the cursor back to column one.

How do you overwrite a previous printed line in Python?

Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within the print statement as print(string, end = "\r") . This returns the next stdout line to the beginning of the line without proceeding to the next line.


1 Answers

You'll need to use a library that lets you write to screen coordinates. I've written the doscmd-screen package to do this (http://doscmd-screen.readthedocs.org/), and it would work something like:

import screen, time
scr = screen.Screen()
scr.writexy(scr.left, scr.top, "CAT DOG MOUSE HORSE")
time.sleep(10)
scr.writexy(scr.left, scr.top, " " * scr.width)

Note: the package was originally only for dos, but now also supports *nix.

like image 114
thebjorn Avatar answered Nov 03 '22 08:11

thebjorn