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

來源:互聯網
上載者:User

標籤: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學習--課本程式練習(周更)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.