0

I'm not exactly sure how to word this to be honest, but here's a simplified version of my current code.

for idx in range(0,11):
    print('Failed:',idx)

This outputs:

Failed: 0
Failed: 1
Failed: 2
Failed: 3
Failed: 4
Failed: 5
Failed: 6
Failed: 7
Failed: 8
Failed: 9
Failed: 10

I'm trying to replace the original int with the next int in the range instead of spamming my terminal with a new line for every int in the range.

How could I update "Failed: 1" to "Failed: 2" without creating a new line for "Failed: 2"?

Sorry if this is poorly worded, if you're willing to help me but like me to elaborate further please say so and I will try my best.

TrevoR
  • 3
  • 2
  • I think there is a `system.flush` method. You could also use `os.system("clear")` or `os.system("cls")`on Windows. – Banana May 29 '20 at 08:10

4 Answers4

0

What you probably want to do is change 'Failed: 1' to 'Failed: 2', not have 'Failed: 1 Failed: 2' on the same line or new lines.

The ASCII backslash b (backspace) helps erase characters on the same line. You'd want to move the cursor back to where the last number was and replace it.

Here's a simple program that moves back on the same line and replaces the counter.

import time
import sys

print(0, end="")
prev = 0
for i in range(20):
    print(("\b" * (len(str(prev)) + 1)), i, end="")
    sys.stdout.flush()
    prev = i

    time.sleep(1)

And to complete your example:

import time
import sys

last_idx = None
prefix = ''
print("Failed: ", end="")
for idx in range(0,11):
    if last_idx is not None:
        prefix = '\b' * len(str(last_idx))

    print(prefix + str(idx), end='')
    # python buffers output so you want to flush it everytime.
    sys.stdout.flush()
    last_idx = idx
    # I've added a time.sleep here so you can see it happen rather than zoom past you
    time.sleep(1)

Alternatively, instead of just backspacing few characters, you can go back to the beginning of the line with "carriage return" (\r instead of \b).

algrebe
  • 1,621
  • 11
  • 17
  • Thanks man, this worked perfectly for me. I also appreciate you taking the time to explain some things as well. – TrevoR May 29 '20 at 08:25
0

Try this :

import sys
import time

for i in range(10):
    sys.stdout.write("\rFailed: {}".format(i))
    sys.stdout.flush()
    time.sleep(0.5)
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
0

There is a second parameter that the function print takes to to specify what should print do at the end of line

print("Text here", end="")
medmik
  • 1
-1

whoops let's try again...

Your only option is to clear the whole console between prints. there are two ways of going about this.

option 1: You could try and use ANSI escape sequence but might be a little flakey

print(chr(27)+'[2j')
print('\033c')
print('\x1bc')

Option 2: use the os module

from os import system, name 
# Only on Unix
system('cls') 
# Only on windows
system('clear')
James
  • 56
  • 8
  • This does not solve problem, this only make all printed word compressed into one line, but does not remove the one printed before. – okie May 29 '20 at 08:16