python程序实例讲解_Python语言程序设计基础(2)—— Python程序实例解析

温度转换

deftempConvert(ValueStr):if ValueStr[-1] in ['F','f']:

ans= (eval(ValueStr[0:-1]) - 32)/1.8

print("{:.2f}C".format(ans))elif ValueStr[-1]=='C' or ValueStr[-1]=='c':

ans= 1.8*eval(ValueStr[0:-1]) + 32

print("{:.2f}F".format(ans))else:print("格式错误")

str=input()

tempConvert(str)

画蟒蛇

importturtledefdrawSnake(radius,angle,length):

turtle.seth(-angle/2)for i inrange(length):

turtle.circle(radius,angle)

turtle.circle(-radius,angle)

turtle.circle(radius,angle/2)

turtle.fd(40)

turtle.circle(16,180)

turtle.fd(40*2/3)

turtle.setup(0.5,0.5)

turtle.penup()

turtle.fd(-250)

turtle.pendown()

turtle.pensize(25)

turtle.pencolor("purple")

drawSnake(40,120,4)

turtle.done()

习题部分

温度转换,输出整数

deftempConvert(ValueStr):print(ValueStr)if ValueStr[-1] in ['F','f']:

ans= (eval(ValueStr[0:-1]) - 32)/1.8

print("{:.0f}C".format(ans))elif ValueStr[-1]=='C' or ValueStr[-1]=='c':

ans= 1.8*eval(ValueStr[0:-1]) + 32

print("{:.0f}F".format(ans))else:print("格式错误")

tempConvert(eval(input()))

美元转换

defcalc(x):if x[-1]=='$':return eval(x[0:-1])*6

elif x[-1]=='¥':return eval(x[0:-1])/6

else:return -1money=input()print(calc(money),end='')if money[-1]=='$':print('¥')elif money[-1]=='¥':print('$')else:print("errer")

彩色蛇

importturtledefdrawSnake(radius,angle,length):

turtle.seth(-angle/2)

color= ["grey","gold","violet","purple","black"]for i inrange(length):

turtle.pencolor(color[i])

turtle.circle(radius,angle)

turtle.circle(-radius,angle)

turtle.pencolor(color[-1])

turtle.circle(radius,angle/2)

turtle.fd(40)

turtle.circle(16,180)

turtle.fd(40*2/3)

turtle.setup(0.5,0.5)

turtle.penup()

turtle.fd(-250)

turtle.pendown()

turtle.pensize(25)#turtle.pencolor("purple")

drawSnake(40,120,4)

turtle.done()

三角形

importturtleimportmathdefpaintS():

turtle.setup(0.5,0.5)for i in range(3):

turtle.seth(120*i)

turtle.forward(50)

turtle.done()

四边形

defpaintX():

turtle.setup(0.5,0.5)

turtle.pensize(5)

turtle.pencolor("gold")for i in range(4):

turtle.seth(i*90)

turtle.penup()

turtle.forward(20)

turtle.pendown()

turtle.forward(60)

turtle.penup()

turtle.forward(20)

turtle.done()

paintX()

六角形

importturtleimportmathdefpaint():

turtle.pensize(5)

x= -30turtle.seth(x)for i in range(1,4):

turtle.forward(150)

turtle.seth(x+120)

x+=120turtle.penup()

turtle.seth(-60)

turtle.forward(50*math.sqrt(3))

turtle.pendown()

x= 30turtle.seth(x)for i in range(1,4):

turtle.forward(150)

turtle.seth(x+120)

x+=120turtle.done()

paint()

正方形螺旋线

importturtleimportmathdefpainZ():

turtle.pensize(2)

cnt=0

x= 10

for i in range(1,60):

turtle.seth(90*i)if cnt >=2:

x+=10cnt=0

turtle.forward(x)

cnt+=1turtle.done()

painZ()

你可能感兴趣的:(python程序实例讲解)