print(foo, end=“”) not working in terminal

后端 未结 1 1344
我在风中等你
我在风中等你 2021-01-22 05:03

So this is some code that is supposed to print text, similar to how Pokemon does. Purely for fun.

The problem is that print(x, end=\"\") does not work when

1条回答
  •  渐次进展
    2021-01-22 05:57

    This is happening because python uses a buffer to write to stdout. in order to get the desired effect, you must put sys.stdout.flush() at the end of your code...

    import time, sys
    
    lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    
    for x in lorem:
        print(x, end="")
        time.sleep(0.03)
        sys.stdout.flush()
    

    This will print out each character individually at the rate of 1 character per 0.03 seconds

    0 讨论(0)
提交回复
热议问题