問題
0から9までの数字を1秒おきに表示させたい。
count.py
# -*- coding: utf-8 -*-
import time
i = 0
while(i<9):
print(i, end='')
time.sleep(1)
i+=1
実行。
% python3 count.py
...あれ、何もprint
されないし何秒たっても何も起こらない...。
試しに
count.py
# -*- coding: utf-8 -*-
import time
i = 0
while(i<10):
print(i)
time.sleep(1)
i+=1
としてみる(引数end
を消してみる)と、
% python3 count.py
0
1
2
3
4
5
6
7
8
9
%
1秒おきに表示された。ただ、end
は指定したい...。
解決
引数flush
を持たせる。
count.py
# -*- coding: utf-8 -*-
import time
i = 0
while(i<10):
print(i, end='', flush=True)
time.sleep(1)
i+=1
実行してみると、
% python3 count.py
0123456789%
うまくいきました。