標籤:cloc 時間 -- .com one image hid 計時 分享
1.繪製正方形螺旋線
import turtleturtle.setup(600,300,200,200)turtle.pensize(1)turtle.color(‘green‘)i=0while i<160: turtle.seth(90) turtle.fd(i+1) turtle.seth(180) turtle.fd(i+2) turtle.seth(-90) turtle.fd(i+3) turtle.seth(0) turtle.fd(i+4) i=i+4turtle.seth(90)turtle.fd(161)turtle.seth(180)turtle.fd(162)turtle.seth(-90)turtle.fd(163)
View Code
2.繪製無角正方形
import turtledef drawtriangle(angle): turtle.seth(angle) turtle.penup() turtle.fd(20) turtle.down() turtle.fd(80) turtle.penup() turtle.fd(20) turtle.down() turtle.setup(800,350)turtle.pensize(1)turtle.pencolor("black")for i in (0,90,180): drawtriangle(i)turtle.seth(-90)turtle.penup() turtle.fd(20)turtle.down()turtle.fd(80)View Code
3.每周工作五天,休息2天,休息日水平下降0.01,工作日要努力到什麼程度,一年後的水平才和每天努力1%取得的效果一樣。
#函數編程的思想#DayDayupimport mathdef dayUp(df): dayup=1.0 for i in range(365): if i%7 in[6,0]: dayup=dayup*(1-0.01) else: dayup=dayup*(1+df) return dayupdayfactor=0.01while(dayUp(dayfactor)<37.78): dayfactor+=0.001 print("dayfactoris:{:.3f}.".format(dayfactor))View Code
4.繪製進度條
1.多行累積不重新整理(time.sleep延時輸出)
#Textprogress barimport timeprint("-----執行開始-----")scale=10for i in range(scale+1): c=(i/scale)*100 a,b="**"*i,".."*(scale-i) print("%{:^3.0f}[{}>-{}]".format(c,a,b)) time.sleep(0.1)print("-----執行開始-----") View Code
2.單行重新整理 print函數中添加參數end=‘‘不換行 頭部加入逸出字元‘\r’把輸出指標移到行首部 註:IDLE屏蔽單行重新整理功能,需運行.py程式
#Textprogress barimport timeprint("-----執行開始-----")scale=10for i in range(scale+1): c=(i/scale)*100 a,b="**"*i,".."*(scale-i) print("\r%{:^3.0f}[{}>-{}]".format(c,a,b),end=‘‘) time.sleep(0.1)print("-----執行結束-----") View Code
3.以上基礎上增加啟動並執行時間監控 time.clock()函數 第一次調用計時開始,第二次及後續調用返回與第一次記時的時間差
#Textprogress barimport timeprint("-----執行開始-----")scale=10t=time.clock()for i in range(scale+1): c=(i/scale)*100 t-=time.clock() a,b="**"*i,".."*(scale-i) print("\r%{:^3.0f}[{}>-{}]{:.2f}s".format(c,a,b,-t),end=‘‘) time.sleep(1)print("-----執行結束-----") View Code
Python學習--課本程式練習(周更)