1. 程式人生 > >Python學習--課本程序練習(周更)

Python學習--課本程序練習(周更)

tor bar 工作日 刷新 () 一次 ima pos else

1.繪制正方形螺旋線

技術分享圖片

技術分享圖片
import turtle
turtle.setup(600,300,200,200)
turtle.pensize(1)
turtle.color(green)
i=0
while 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+4
turtle.seth(90)
turtle.fd(161)
turtle.seth(
180) turtle.fd(162) turtle.seth(-90) turtle.fd(163)
View Code

2.繪制無角正方形

技術分享圖片

技術分享圖片
import turtle
def 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%取得的效果一樣。

技術分享圖片
#函數編程的思想
#DayDayup
import math
def 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 dayup dayfactor=0.01 while(dayUp(dayfactor)<37.78): dayfactor+=0.001 print("dayfactoris:{:.3f}.".format(dayfactor))
View Code

4.繪制進度條

1.多行累積不刷新(time.sleep延時輸出)

技術分享圖片
#Textprogress bar
import time
print("-----執行開始-----")
scale=10
for 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 bar
import time
print("-----執行開始-----")
scale=10
for 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 bar
import time
print("-----執行開始-----")
scale=10
t=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學習--課本程序練習(周更)