基础练习

练习

  1. 使用turtle库,绘制一个八边形。图形如下所示:

八边形.jpg

import turtle as t
"""
@author RansySun
@create 2019-07-15-15:12
"""
t.setup(800, 400)
t.pensize(4)

for i in range(8):
    t.left(45)
    t.fd(60)

t.done()
  1. 使用turtle库,绘制一个八角图形。图形如下所示:

八角图形.jpg

import turtle as t
"""
@author RansySun
@create 2019-07-15-15:17
"""
t.setup(800, 400)
for i in range(8):
    t.right(135)
    t.fd(100)
t.done()
  1. 简述import <模块名>/from <模块名> import */import <模块名> as <新模块名>三者的区别
import <模块名>:导入所需要的模块名,每次使用其中的模块都要使用模块名调用相应的函数,比较繁琐;
from <模块名> import *:引用其中的模块不需要使用模块名,但是容易出现变量和模块之间的冲突;
import <模块名> as <新模块名>:将导入的模块重新命名,简写了模块的重复,方便调用其中模块;
  1. 设计程序,要求:循环打印数列1,3,5,...,99
for i in range(100):
    if i % 2 == 1:
        print(i)
  1. 使用turtle库,绘制的图形,,如下图:

花

import turtle as t
import random
"""
@author RansySun
@create 2019-07-15-15:59
"""
t.setup(800, 400)
# 上移
t.penup()
t.right(90)
t.fd(-100)
t.left(90)
t.fd(-80)
t.pendown()

for x in range(1, 38):
    color1 = random.randint(0, 255)
    color2 = random.randint(0, 255)
    color3 = random.randint(0, 255)
    t.colormode(255)
    t.pencolor(color1, color2, color3)
    t.fd(150)
    t.left(170)
# 画花心
t.penup()
t.fd(76)
t.pendown()
t.pencolor("red")
t.pensize(10)
t.circle(1)

# 画秸秆
t.left(100)
t.pensize(5)
t.pencolor("green")
t.fd(250)

t.bk(40)
t.right(-90)
t.fd(10)

# 画叶子
#
t.seth(-30)
a = 0.3
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)
        t.fd(a)
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.fillcolor("green")
t.end_fill()

# 画左边叶子
t.left(120)
t.penup()
t.fd(60)
t.pendown()
t.left(90)

t.penup()
t.fd(20)
t.pendown()

t.seth(30)
a = 0.3
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)
        t.fd(a)
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.fillcolor("green")
t.end_fill()

t.left(60)

t.penup()
t.fd(-120)
t.right(90)
t.fd(-160)
t.pendown()

# 画水
t.pencolor("blue")
t.seth(-40)
for i in range(4):
    t.circle(40, 80)
    t.circle(-40, 80)

t.seth(10)

t.penup()
t.fd(-380)
t.right(90)
t.fd(10)
t.pendown()

t.seth(-40)
for i in range(4):
    t.circle(40, 80)
    t.circle(-40, 80)

t.left(90)

t.done()

posted @ 2019-07-15 22:22  RandySun  阅读(224)  评论(0编辑  收藏  举报