0

I am trying to print multiple lines while updating them, for example:

Attempts1: 10
Attempts2: 10

I've tried this:

from time import sleep
import subprocess

while True:
  for number in range(100):
    print("Attempts1: {}".format(number))
    print("Attempts2: {}".format(number))
    sleep(0.1)
    subprocess.call("clear")

but the text keeps flashing and that's not what I want.

So then I tried this:

from time import sleep

while True:
  for number in range(100):
    print("Attempts1: {}".format(number), end='\r', flush=True)
    print("Attempts2: {}".format(number), end='\r', flush=True)
    sleep(0.1)

And get this:

Attempts1:1
Attempts2:1
Attempts1:2
Attempts2:2

And So On

soliify
  • 21
  • 1
  • The text keeps flashing because you clear the screen 10 times/sec. If you don't want it to flash, then use cursor control characters to overwrite the text instead of rewriting the entire screen. See the cited duplicate for a starting point. – Prune Apr 10 '20 at 00:37
  • I've used the cited duplicate already and it works, but I am trying to do it with multiple lines, not just one. – soliify Apr 10 '20 at 00:46
  • You have to extend what the duplicate shows and use vertical cursor movement characters as well. – Prune Apr 10 '20 at 03:24

1 Answers1

0

Your first attempt works if you change the order

import os
while True:
    for number in range(100):
        os.system("clear")

        print("Attempts1: {}".format(number))
        print("Attempts2: {}".format(number))
        sleep(0.1)

Hope it helps.