Why print in Python doesn't pause when using sleep in a loop?

后端 未结 2 794
Happy的楠姐
Happy的楠姐 2020-11-29 10:00

This code:

import time
for i in range(10):
    print(i)
    time.sleep(.5)

Causes my computer to hang for 5 seconds, and then print out 0-9

2条回答
  •  攒了一身酷
    2020-11-29 10:05

    Try this:

    for i in range(10):
            sys.stdout.write('\r' + str(i))
            time.sleep(.5)
    

    here '/r' is carriage return, it brings the cursor first place again.

提交回复
热议问题