0

programmers, I have this little code here: import time

def loop(args):
    a = 100.1
    b = 0.1

    a = a - b
    time.sleep(0.2)
    while a != 0:
        a = a - 1
        print(a)

loop(1)

And what I want is to make the print statement change in the screen in realtime instead of printing many times in another line like this: https://i.stack.imgur.com/qJpMC.jpg. If there's any piece of text inside the docs of python, please link it here.

Thanks for your attention.

1 Answers1

1

Avoid the newline and the end of each print(), and use a carriage return instead:

from __future__ import print_function

import time


def loop(args):
    a = 100.1
    b = 0.1

    a = a - b
    while a != 0:
        time.sleep(0.2)
        a = a - 1
        print(a, end='\r')
    print()

loop(1)
Apalala
  • 9,017
  • 3
  • 30
  • 48