[PYTHON] Calling time.sleep () after the print function with the argument end does not print

problem

I want to display the numbers from 0 to 9 every second.

count.py


# -*- coding: utf-8 -*-
import time

i = 0
while(i<9):
    print(i, end='')
    time.sleep(1)
    i+=1

Run.

% python3 count.py

... that, nothing is printed and nothing happens after a few seconds ...

As a test

count.py


# -*- coding: utf-8 -*-
import time

i = 0
while(i<10):
    print(i)
    time.sleep(1)
    i+=1

(Try deleting the argument ʻend`),

% python3 count.py
0
1
2
3
4
5
6
7
8
9
%

Displayed every second. However, I want to specify ʻend` ...

Solution

Have the argument flush.

count.py


# -*- coding: utf-8 -*-
import time

i = 0
while(i<10):
    print(i, end='', flush=True)
    time.sleep(1)
    i+=1

When I run it,

% python3 count.py
0123456789%

It went well.

Recommended Posts

Calling time.sleep () after the print function with the argument end does not print
After installing php7.2, the php command does not work
Check when the version does not switch with pyenv
If you give a list with the default argument of the function ...
python> does not include the letters mm> if "mm" not in text: / print "not including mm"
The module that should have been installed with pip does not run
python> print> Redirected only at the end of processing?> Run with -u