编程技巧(十)(持续更新中)

一、怎么让一个功能在循环中只执行一次

在循环体外设置一个参数 为 False,在循环体中的功能执行后就将这个参数变为True,条件为必须那个参数为False才能运行。循环就不回执行这个功能了。先插入伪代码。

isp = False
while True:
    ...
    ...
    if isp == False:
        ...
        ...
        isp = True
    ...
    ...

二、怎么在屏幕显示进度条

使用sys.stdout.write()因为不用换行输出。

 1 from __future__ import division
 2 import sys,time
 3 
 4 for i in range(50):
 5     #sys.stdout.write('\r')
 6     sys.stdout.write('\r')
 7     #tu = ''.format()
 8     tu = '%s%% |%s'%(int((int(i)/49)*100),int((int(i)/49)*50)*'*')
 9     sys.stdout.write(tu)
10     sys.stdout.flush()
11     time.sleep(0.5)
View Code

 

  

posted on 2016-05-18 21:14  lexn  阅读(87)  评论(0)    收藏  举报

导航