用Python写a穿过#

来源:互联网 发布:linux卸载httpd服务 编辑:程序博客网 时间:2024/06/06 05:02

可以实现的功能是字符a动态穿越了 20个#
展现的是

  • sys.stdout.write(),和print类似,但是不会换行
  • \r覆盖打印
  • sys.stdout.flush()写入硬盘,不然sys.stdout.write()只会写入缓存,看不到输出
  • 异常捕获。当程序在sleep(0.5)的时候,捕获到ctl+c键盘终端,不打印错误信息退出
#!/usr/bin/env pythonimport timeimport syssys.stdout.write('\r'+ '#'*20)sys.stdout.flush()count = 0while 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
0 0
原创粉丝点击