7

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Kye
  • 71
  • 1
  • 3
  • You can't. Once something is printed to the console, it's there for good. If you're willing to go into simple GUI programming, then you could do it. – Zizouz212 Mar 22 '15 at 16:31
  • You could use curses (or some [variant](http://inventwithpython.com/pygcurse/) if you have Windows). – m0dem Mar 22 '15 at 16:39
  • 1
    on windows you can terminate your format string with "\r" instead of "\n" sys.stdout.write("...\r" % (variables) sys.stdout.flush() – Marichyasana Mar 22 '15 at 17:13
  • 2
    @Zizouz212 that's not true. You can carriage return to the start of the line and overwrite – Peter Wood Nov 03 '20 at 00:54
  • 1
    Duplicate: https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python – Tomerikoo Nov 03 '20 at 00:57

2 Answers2

2

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.

thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • Thanks :-) Most of the heavy lifting is done by colorama, I just added functionality to fetch screen information from various system calls.. It is rather convenient in its smallness for doing simple tasks. – thebjorn Mar 22 '15 at 16:51
  • looks complicated :) but thank you. Its actually saying "No module named screen" am I required to download something or install something? (I have imported screen) – Kye Mar 22 '15 at 17:13
  • Yes, you need to follow the installation directions in the docs (linked to in my answer), basically `pip install doscmd-screen`. – thebjorn Mar 22 '15 at 17:20
  • Do you just type "pip install doscmd-screen" into my code? Because if so it says "install" is a syntax error – Kye Mar 22 '15 at 20:05
0

If you're operating on the terminal, you can use os.system('clear') as follows:

import os
import time

# first statement
print('CAT DOG MOUSE HORSE')

# wait then clear
time.sleep(10)
os.system('clear')

#second statement
print('DOG HORSE RABBIT CAT')
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69