您的位置:首页 > 编程语言 > Python开发

用Python写a穿过#

2016-12-16 16:23 120 查看
可以实现的功能是字符a动态穿越了 20个#

展现的是

sys.stdout.write(),和print类似,但是不会换行

\r覆盖打印

sys.stdout.flush()写入硬盘,不然sys.stdout.write()只会写入缓存,看不到输出

异常捕获。当程序在sleep(0.5)的时候,捕获到ctl+c键盘终端,不打印错误信息退出

#!/usr/bin/env python

import time
import sys

sys.stdout.write('\r'+ '#'*20)
sys.stdout.flush()

count = 0
while True:
sys.stdout.write('\r'+ '#' * count + 'a')
sys.stdout.flush()
count += 1
try:
time.sleep(0.5)
except KeyboardInterrupt:
print '\nexit'
break
if count == 20:
sys.stdout.write('\r'+'#' * 20)
sys.stdout.flush()
count = 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python