0003-python第二天:掌握循环在绘图中的应用

目录

一、复习笔记

1.温度转换:

2.数字转换

3.货币转换

二、画图训练

1.画蟒蛇

注1:用下面代码看circle参数区别

注2:turtle.right(angle)和turtle.seth(angle)演示

2.循环画规律几何 

3.图形上色

 4.用goto画立体几何

5.用角度变化画风车

6.送你一颗心


一、复习笔记

1.温度转换:

bug1在 print("{:.2f}C".format(D)) 的传输格式上,这是一个特定格式的填充

bug2在 c = eval(s[0:-1])  没有做字段的去壳操作

2.数字转换

在print(A[eval(c)],end="ti")  中end=""  才能实现增加操作

举例:print的3种输出方式

print("he \n ll \n o") #换行要有引号
s = 'hello'
for i in s:     #用for in模式可以直接实现单字符换行输出
    print(i)    #print(s)是输出5次hello
s = 'hello'
for i in s:
    print(i,end = ",")   #加上=end就可以一行输出了

 

3.货币转换

[:3] 取3前面的,[3:]取3后面的(含3)

二、画图训练

1.画蟒蛇

from turtle import *
setup(650,650,200,200) #确定窗体,可以没有
penup() #抬笔
fd(-120)
pendown()
pensize(50)
pencolor("red")
seth(-40) #坐标轴(窗体内的绝对坐标轴的绝对度数)操作:仅改变方向,不移动
for i in range(2):   #用画圆来描绘蟒蛇身体
    circle(40,80)
    circle(-40,80)
circle(40,40)  #半径,角度
fd(40)
circle(50,90)
right(90)
pensize(5)
fd(20)
exitonclick()  

 

注1:用下面代码看circle参数区别

from turtle import *
circle(100,90)
circle(10)
exitonclick()  

 

 

注2:turtle.right(angle)和turtle.seth(angle)演示

from turtle import *
pensize(10)
fd(120)
right(90)
fd(120)
seth(-180) 
fd(120)
right(90)
fd(-120)
left(90)
fd(-120)
exitonclick()  

 

注意:left、righ和seth转过去的时候,坐标系一起转,所以还可以用fd做正负指标的绘图 

2.循环画规律几何 

from  turtle import *
fd(100)
for i in range(8):     #循环执行8次
    left(80)
    fd(100)

3.图形上色

from turtle import *
pensize(10)
color(0.66,0.88,0.12)    #颜色设置默认为小数,可改类型为整数
fd(120)

 

 

turtle函数中填充颜色有turtle.color()、turtle.pencolor()

from turtle import *
pensize(10)
pencolor(0.66,0.88,0.12)
fd(120)
color(0,0,0.12)
circle(20)

 

 4.用goto画立体几何

from  turtle import *
goto(100,100)
goto(100,200)
goto(200,100)
goto(0,0)
goto(100,200)
penup()
goto(100,100)
pendown()
goto(200,100)
exitonclick()

5.用角度变化画风车

from  turtle import *
penup()
goto(0,0)
pendown()
for i in range(4):
    left(45)
    fd(-100)
    right(90)
    circle(100,45)
    right(-90)
    fd(100)

 

6.送你一颗心

import turtle as t
t.penup()
t.seth(-90)
t.fd(160)
t.pendown()
t.pensize(20)
t.colormode(255)
for j in range(10):
  t.speed(1000)
  t.pencolor(25*j,5*j,15*j)
  t.seth(130)
  t.fd(220)
  for i in range(23):
    t.circle(-80,10)
  t.seth(100)
  for i in range(23):
    t.circle(-80,10)
  t.fd(220)


版权声明:本文为m0_68750710原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。