Alright so I was not sure exactly how to say this, but here is the idea. I have a graph or table I guess you could call that looks like this.

print '''
-----------------------------
|
|
|
|x
|
|
|
|-----------------------------
'''

Then I want to make a change to where x is for example moving it down twice.
How would I go about this without having the first table appear?

Dani AI

Generated

The original question actually splits into two different needs: either the first output should never appear (so simply delay printing the initial text), or a visible chart should be displayed and then updated in-place so the old position of the marker disappears. , and sketched quick terminal hacks that work for small, one-line cases, but for reliable, coordinate-based updates a terminal-control approach is preferable.

A compact, robust option on Unix-like systems is the standard curses API (works from a real terminal, not in IDLE). The example below draws a simple box, places an "x", waits, erases the old "x" and draws it two lines lower. This demonstrates the coordinate-based redraw pattern that avoids printing the whole chart again.

import curses
import time

def main(stdscr):
    stdscr.clear()
    for r in range(9):
        stdscr.addstr(r, 0, '|' + ' ' * 25 + '|')
    stdscr.addch(4, 5, ord('x'))
    stdscr.refresh()
    time.sleep(1)
    stdscr.addch(4, 5, ord(' '))
    stdscr.addch(6, 5, ord('x'))
    stdscr.refresh()
    time.sleep(1)

curses.wrapper(main)

Notes and cautions: terminal-control code must run in a real console; GUI shells often do not emulate terminals well. On Windows, the curses API is not native—installable ports or higher-level libraries provide equivalent behavior. Redrawing only the changed cells (erase old char, draw new char) avoids flicker; for heavy updates use the library's buffered refresh methods to batch screen updates. For the trivial "print 'hi' then later show 'Hello' but never display 'hi'" case, the simplest answer is to avoid emitting the first string at all and only print the final text when ready.

Recommended Answers

All 8 Replies

without having the first table appear?

I do not get it.

I do not get it.

Okay so a simpler example. you do print "hi" then when the script is ran the terminal output hi how do I print hi then like 5 seconds later print "Hello" without the first print statement's output being shown in the terminal.

Something like this:

from __future__ import print_function
import sys
import time

print('Hi!', end='')
time.sleep(5)
print(3*'\b'+'Hello!')
sys.stdout.flush()

Depending on your operating system, there are a couple of ways.

Start with

import os

in your script.

If you are on Linux (also Mac I think) you can do:

os.system("clear")

to clear the terminal screen.

If you are on Windows you can do:

os.system("cls")

Note that these probably won't work on IDLE or similar, but will work from the terminal. Clear the screen, and redraw your chart.

Thanks very much pyTony that is not something I even knew existed. Where did you learn python from?

Thanks to you as well nosehat you commented as I posted my last one. Where did you learn this?

Thanks very much pyTony that is not something I even knew existed. Where did you learn python from?

I learned it basically from online tutorials, but real learning happens when you "play with the language". I do know several languages before starting with TI-58C programmable calculator, where the language is basically key codes as numbers and floating point values, basically assembly level language, 1981 as school boy of 8th grade.

To clear the screen in a linux terminal, use this

print("\x1B[2J")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.