实际案例如下:
1. 动态打印三条时间信息
def t_clock():
cnt = 0
import time
while True:
s = time.asctime()
print(s[11:19],end="")
time.sleep(1)
cnt += 1
if(cnt>=3):
return
print("\r",end="",flush = True)
t_clock()
2. 重写睡眠函数
import time
def sleep(seconds):
s = seconds
while seconds > 0:
print('还剩%s秒' % seconds, end="")
time.sleep(1)
print("\r", end="", flush=True)
seconds -= 1
print('已完成%s秒的休息' % s)