贵州做网站怎么推广,建设网站 费用,网页美工设计培训学校,wordpress+html5播放优酷1. 统计一个英文单词的集合中包含全部是小写字母的单词总数。
strings { cad, PE , Window, FM, hello, world,flowers }
n 0
for word in strings :if word.islower() :n 1
print(n)2. 根据列表中保存的数据采用turtle库画图直方图#xff0c;显示输出在屏幕上#xff0…1. 统计一个英文单词的集合中包含全部是小写字母的单词总数。
strings { cad, PE , Window, FM, hello, world,flowers }
n 0
for word in strings :if word.islower() :n 1
print(n)2. 根据列表中保存的数据采用turtle库画图直方图显示输出在屏幕上效果如下图所示。 import turtle as t
ls [69, 292, 33, 131, 61, 254]
X_len 400
Y_len 300
x0 -200
y0 -100t.penup() #抬起画笔移动时不绘制轨迹。
t.goto(x0, y0) #将画笔移动到指定坐标(x0, y0)
t.pendown() #放下画笔移动时绘制轨迹。t.fd(X_len) #从x、y轴的交点出发沿x轴移动400像素
t.fd(-X_len) #沿x轴端点往回绘制最终返回到x、y轴的交点
t.seth(90) #改变海龟朝向准备绘制y轴
t.fd(Y_len) #从x、y轴的交点出发沿y轴移动300像素t.pencolor(red) #设置画笔颜色
t.pensize(5) #设置画笔宽度
for i in range(len(ls)) :t.penup()t.goto(x0 (i 1) * 50, y0)t.seth(90) #设置海龟朝向,垂直向上绘制直线t.pendown()t.fd(ls[i]) #移动距离为列表里的数值
t.done()3. 利用random库和turtle库在屏幕上绘制5个圆圈圆圈的半径和圆初始坐标由randint()函数产生圆的X和Y坐标范围在[-100,100]之间半径的大小范围在[20,50]之间圆圈的颜色随机在color列表里选择。 import turtle as t
import random as r
color [red,orange,blue,green,purple]
r.seed(1)
for i in range(5):rad r.randint(20,50) #半径x0 r.randint(-100,100) #圆心坐标(x0,y0)y0 r.randint(-100,100)t.color(r.choice(color)) #圆圈的颜色t.penup()t.goto(x0,y0)t.pendown()t.circle(rad)
t.done()random.choice()是一个用于从给定的序列中随机选择一个元素的函数。 具体来说它从一个可迭代对象如列表、元组或字符串中随机返回一个元素。 例 import randomoptions [Option A, Option B, Option C]
random_choice random.choice(options)print(Randomly selected option:, random_choice)
#输出结果从options列表中随机选择一个元素并将其打印出来。每次运行程序选择的结果可能不同因为它是随机的。4. 使用turtle库的turtle.fd()函数和turtle.left()函数绘制一个边长为200像素的正方形及一个紧挨四个顶点的圆形。 import turtle
turtle.pensize(2)
for i in range(4) :turtle.fd(200)turtle.left(90)
turtle.left(-45)
turtle.circle(100 * pow(2, 0.5)) #半径正方形对角线长度的一半根号2*正方形边长*1/25. 利用random库和turtle库在屏幕上绘制4个小雪花雪花的中心点坐标由列表points给出雪花的半径长度由randint)函数产生。雪花的颜色是红色。 import turtle as t
import random as rr.seed(1)
t.pensize(2)
t.pencolor(red)
angles 6 #六边形雪花
points [[0,0],[50,40],[70,80],[-40,30]] #雪花中心点的坐标for i in range(4): #绘制4个小雪花x0,y0 points[i]t.penup()t.goto(x0,y0)t.pendown()length r.randint(6, 16) #雪花半径长度for j in range(angles): #六条边t.fd(length) #从中心点出发绘制t.backward(length) #画完边后返回中心点t.right(360 / angles) #每次改变60°顺时针
t.done()6. 使用turtle库绘制三个彩色的圆圆的颜色按顺序从颜色列表color中获取圆的圆心位于(0.0)坐标处半径从里至外分别是10像素30像素60像素。 import turtle as t
color [red,green,blue]
rs [10,30,60] #圆的半径for i in range(3): #绘制三个圆t.penup()t.goto(0, -rs[i]) #从圆的底部开始逆时针绘制图形t.pd()t.pencolor(color[i])t.circle(rs[i])
t.done()7. 使用turtle库函数绘制4个等距排列的正方形边长为40像素间距宽度为40。最左边的正方形左上角坐标为(0.0)。
import turtle
n 4
for j in range(n):turtle.pendown()for i in range(4):turtle.fd(40)turtle.right(90)turtle.penup()turtle.fd(80)
turtle.done()8. 使用turtle库在屏幕上绘制正方形及其内接圆形。其中正方形左下角坐标为(0, 0)边长为黑色长度200像素线宽1像素﹔内切圆为绿色线宽5像素内部以红色填充。 import turtle as t
for i in range(4):t.fd(200) #起点在正方形左下角t.left(90)
t.penup()
t.goto(100,0) #将画笔移动到底部中心点
t.pendown()
t.color(green,red) #绿色描边黑色填充
t.pensize(5)
t.begin_fill()
t.circle(100)
t.end_fill()
t.done()9. 实现下面功能(1)使用turtle库和random库在屏幕上绘制5个彩色的圆(2)圆的颜色随机从颜色列表color中获取(3)圆的起始坐标x和y值从范围[-100,100]之间选取半径从范围[10,30]之间选取。
import turtle as t
import random as rcolor [red,green,blue,purple,black]
r.seed(1)
for j in range(5):t.pencolor(color[r.randint(0,4)])t.penup()t.goto(r.randint(-100,100),r.randint(-100,100))t.pendown()t.circle(r.randint(10,30))
t.done()10. 使用turtle库和random库绘制四个彩色的正方形正方形颜色随机从颜色列表color中获取。正方形边长从范围[50,20]之间选取每个正方形左下角坐标x如和y从范围[-100,100]之间选取。
import turtle as t
import random as r
color [red,blue,purple,black]
r.seed(1)
for j in range(4):t.pencolor(color[r.randint(0,3)])t.penup()t.goto(r.randint(-100,100), r.randint(-100,100))t.pendown()ra r.randint(50, 200)for i in range(1,5):t.fd(ra)t.seth(90 * i)
t.done()总结
在使用Python中的turtle第三方库进行绘图时以下是一些常用的函数
turtle.forward(distance)向当前方向移动指定距离。缩写为turtle.fd()turtle.backward(distance)向相反方向移动指定距离。turtle.left(angle)左转指定角度逆时针。turtle.right(angle)右转指定角度顺时针。turtle.penup()抬起画笔移动时不绘制轨迹。turtle.pendown()放下画笔移动时绘制轨迹。缩写为turtle.pd()turtle.pencolor(color)设置画笔颜色。turtle.pensize(width)设置画笔宽度。turtle.circle(radius)绘制圆形。turtle.clear()清除turtle图形绘制的内容。turtle.done()完成图形绘制显示图形窗口。turtle.goto(x, y)将画笔移动到指定坐标(x, y)turtle.seth()设置海龟朝向角度方向