Python学习--课本程序练习(周更)

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

 4.判断数字是否是回文数

a=eval(input("Enter a unmber:"))
b=a
t=0
while a!=0:
    t=t*10+a%10
    a=a//10
if b==t:
    print("true")
else:
    print("no")
View Code

 

posted @ 2018-01-25 23:04  缥缈一叶舟  阅读(604)  评论(0编辑  收藏  举报