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` ...
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