栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实验、python简单程序及turtle库

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实验、python简单程序及turtle库

实验代码

1.编写程序,实现摄氏度和华氏度之间的温度转换。

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.编写程序,实现人民币和美元之间的钱币转换。

TempStr = input("请输入带有符号的钱币值: ")
if TempStr[-1] in ['¥', 'Y', 'y']:
    C = eval(TempStr[0:-1]) / 6.3
    print("转换后的美元是{:.2f}$".format(C))
elif TempStr[-1] in ['$', 'S', 's']:
    F = eval(TempStr[0:-1]) * 6.3
    print("转换后的人民币是{:.2f}¥".format(F))
else:
    print("输入格式错误")

3.绘制三角形、四边形、五边形、六边形等图形。

import turtle

turtle.setup(650, 650, 300, 300)
#三角形
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(5)
turtle.pencolor("blue")
for i in range(3):
    turtle.fd(90)
    turtle.right(120)

#四边形
turtle.penup()
turtle.home()
turtle.fd(-100)
turtle.pendown()
for i in range(4):
    turtle.fd(90)
    turtle.right(90)

#五边形
turtle.penup()
turtle.home()
turtle.fd(50)
turtle.pendown()
for i in range(5):
    turtle.fd(90)
    turtle.right(72)

#六边形
turtle.penup()
turtle.home()
turtle.left(90)
turtle.fd(100)
turtle.pendown()
for i in range(6):
    turtle.fd(90)
    turtle.right(60)

turtle.done()

4.绘制半径为10、20、40、80,不同颜色的同心圆。

import turtle
import random


def random_color():
    color_list=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ''
    for i in range(6):
        color_number = color_list[random.randint(0,15)]
        color += color_number
    color = '#' +color
    return color

turtle.pensize(10)
for i in range(4):
    turtle.color(random_color())
    turtle.circle(20+30*i)
    turtle.penup()
    turtle.right(90)
    turtle.fd(30)
    turtle.pendown()
    turtle.left(90)

turtle.done()

5.绘制Python蟒蛇。

import turtle

turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("red")
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)
turtle.done()

6.绘制五角星及国旗图案。

import turtle

turtle.setup(800,600,10,10)
tt = turtle.Turtle()
tt.pensize(1)
tt.speed(10)
tt.pencolor('red')
tt.fillcolor('red')
tt.penup()
tt.goto(-300,-200)
tt.pendown()
tt.begin_fill()
tt.goto(300,-200)
tt.goto(300,200)
tt.goto(-300,200)
tt.goto(-300,-200)
tt.end_fill()
tt.penup()
tt.pencolor('yellow')
tt.fillcolor('yellow')
tt.goto(-260,130)
tt.pendown()
tt.begin_fill()
for i in range(0,5):
    tt.forward(40)
    tt.left(72)
    tt.forward(40)
    tt.right(144)
tt.end_fill()
tt.penup()
tt.goto(-120,170)
tt.pendown()
tt.begin_fill()
tt.right(20)
for i in range(0,5):
    tt.forward(10)
    tt.left(72)
    tt.forward(10)
    tt.right(144)
tt.end_fill()
tt.penup()
tt.goto(-90,130)
tt.left(30)
tt.pendown()
tt.begin_fill()
for i in range(0,5):
    tt.forward(10)
    tt.left(72)
    tt.forward(10)
    tt.right(144)
tt.end_fill()
tt.penup()
tt.goto(-94,90)
tt.right(10)
tt.pendown()
tt.begin_fill()
for i in range(0,5):
    tt.forward(10)
    tt.left(72)
    tt.forward(10)
    tt.right(144)
tt.end_fill()
tt.penup()
tt.goto(-120,60)
tt.right(15)
tt.pendown()
tt.begin_fill()
for i in range(0,5):
    tt.forward(10)
    tt.left(72)
    tt.forward(10)
    tt.right(144)
tt.end_fill()
tt.penup()
tt.goto(400,400)

turtle.done()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号