开发者

problem with python print function

开发者 https://www.devze.com 2023-01-16 10:44 出处:网络
I am trying to this function: def sleep(s开发者_运维技巧ec): for i in range(sec): print(\".\", end=\" \");

I am trying to this function:

def sleep(s开发者_运维技巧ec):
    for i in range(sec):
        print(".", end=" ");
        time.sleep(1);

the problem is that it waits for the for loop to finish then it prints everything. If I use the normal print with \n in the end everything works as it should. But with the end=" " it does not.


The stdout is line buffered. You need to flush the output manually.

import sys

def sleep(sec):
    for i in range(sec):
        print(".", end=" ")
        sys.stdout.flush()
        time.sleep(1)
0

精彩评论

暂无评论...
验证码 换一张
取 消