温度转换
def tempConvert(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)
画蟒蛇
import turtle def drawSnake(radius,angle,length): turtle.seth(-angle/2) for i in range(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()
习题部分
温度转换,输出整数
def tempConvert(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()))
美元转换
def calc(x): if x[-1]=='$': return eval(x[0:-1])*6 elif x[-1]=='¥': return eval(x[0:-1])/6 else : return -1 money = input() print(calc(money),end='') if money[-1]=='$': print('¥') elif money[-1]=='¥': print('$') else : print("errer")
彩色蛇
import turtle def drawSnake(radius,angle,length): turtle.seth(-angle/2) color = ["grey","gold","violet","purple","black"] for i in range(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()
三角形
import turtle import math def paintS(): turtle.setup(0.5,0.5) for i in range(3): turtle.seth(120*i) turtle.forward(50) turtle.done()
四边形
def paintX(): 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()
六角形
import turtle import math def paint(): turtle.pensize(5) x = -30 turtle.seth(x) for i in range(1,4): turtle.forward(150) turtle.seth(x+120) x+=120 turtle.penup() turtle.seth(-60) turtle.forward(50*math.sqrt(3)) turtle.pendown() x = 30 turtle.seth(x) for i in range(1,4): turtle.forward(150) turtle.seth(x+120) x+=120 turtle.done() paint()
正方形螺旋线
import turtle import math def painZ(): turtle.pensize(2) cnt = 0 x = 10 for i in range(1,60): turtle.seth(90*i) if cnt >=2 : x+=10 cnt = 0 turtle.forward(x) cnt+=1 turtle.done() painZ()