2-1温度转换(if)
TempStr = input("请输入带有符号的温度值: ")
if TempStr[-1] in ['F', 'f']:
C = (eval(TempStr[0:-1]) - 32) / 1.8
print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
F = 1.8 * eval(TempStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
else:
print("输入格式错误")
2-2温度转换(while)
TempStr = input("请输入带有符号的温度值: ")
while TempStr[-1] not in ['N','n']:
if TempStr[-1] in ['F','f']:
C = (eval(TempStr[0:-1]) - 32)/1.8
print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
F = 1.8*eval(TempStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
else:
print("输入格式错误")
TempStr = input("请输入带有符号的温度值: ")
2-3温度转换(定义函数)
def tempConvert(ValueStr):
if ValueStr[-1] in ['F', 'f']:
C = (eval(ValueStr[0:-1]) - 32) / 1.8
print("转换后的温度是{:.2f}C".format(C))
elif ValueStr[-1] in ['C', 'c']:
F = 1.8 * eval(ValueStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
else:
print("输入格式错误")
TempStr = input("请输入带有符号的温度值: ")
tempConvert(TempStr)
2-4python蟒蛇绘制
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
turtle.circle(40, 80)
turtle.circle(-40, 80)
turtle.circle(40, 80 / 2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2 / 3)
2-5python蟒蛇绘制(函数名)
from turtle import *
setup(650, 350, 200, 200)
penup()
fd(-250)
pendown()
pensize(25)
pencolor("purple")
seth(-40)
for i in range(4):
circle(40, 80)
circle(-40, 80)
circle(40, 80 / 2)
fd(40)
circle(16, 180)
fd(40 * 2 / 3)
2-6python蟒蛇绘制(函数封装)
import turtle
def drawSnake(radius, angle, length):
turtle.seth(-40)
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(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
drawSnake(40, 80, 4)
turtle.done()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)