练习多数来自于 北京理工大学 嵩天等老师的 公开课
温度转换
#tempconver.py
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("输入格式有误")
字符方向编辑
#helloworld1
n = eval(input())
if n == 0:
print("Hello World")
elif n > 0:
print("He\nll\no \nWo\nrl\nd")
else:
print ("H\ne\nl\nl\no\n \nW\no\nr\nl\nd")
四则运算
string = input()
op_num = 0
op = ['+', '-', '*', '/']
for i, s in enumerate(string):
if s in op and i!=0:
op_num = i
if string[0] == '-':
num1 = -float((string[1:i]).strip())
elif string[0:2] == '0x':
num1 = int(eval_r((string[:i]).strip()))
else:
num1 = float((string[0:i]).strip())
num2 = float((string[i+1:]).strip())
if s == op[0]:
print('{:.2f}'.format(num1 + num2))
elif s == op[1]:
print('{:.2f}'.format(num1 - num2))
elif s == op[2]:
print('{:.2f}'.format(num1 * num2))
else:
print('{:.2f}'.format(num1 / num2))
海龟库练习
python
#pythondraw
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(18)
turtle.pencolor("black")
turtle.seth(-40)
for i in range (4):
turtle.circle(40, 80)
turtle.circle(-40,80)
turtle.circle(40, 80/2)
turtle.fd(50)
turtle.circle(14, 180)
turtle.fd(40*2/3)
turtle.done()
方向练习
import turtle
turtle.setup(1000.800,500.200)
turtle.left(45)
turtle.fd(150)
turtle.right(135)
turtle.fd(300)
turtle.left(135)
turtle.fd(150)
空间坐标
#坐标练习
import turtle
turtle.setup(400,400)
turtle.goto(100,100)
turtle.goto(100,-100)
turtle.goto(-100,-100)
turtle.goto(-100,100)
turtle.goto(0,0)
海龟运动函数练习
#海龟运行实验品
import turtle as t
t.setup(1200,600,100,100)
t.penup()
t.fd(-300)
t.pendown()
t.pensize(35)
t.seth(-45)
t.pencolor("green")
for i in range(5):
t.circle(50,90)
t.circle(-50,90)
t.circle(50,30)
t.fd(70)
t.circle(50, 195)
t.fd(100)
t.done()
运动函数练习 ♥
# a hearty present to 小冒
from turtle import *
pendown()
pensize(20)
def curvemove():
for i in range(200):
right(1)
forward(1)
color("red", "pink")
begin_fill()
left(140)
fd(111.65)
curvemove()
left(120)
curvemove()
fd(111.65)
end_fill()
done()
八边形绘制
import turtle as t
t.pensize(2)
for i in range(8):
t.fd(100)
t.left(45)
t.done
八角形绘制
import turtle as t
t.pensize(2)
for i in range(8):
t.fd(150)
t.left(135)