• 条件、循环、函数定义 练习


    1、用循环画五角星

    import turtle
    turtle.color("yellow")
    turtle.fillcolor("yellow")
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(100)
        turtle.right(144)
    turtle.end_fill()

    2、用循环画同心圆

    from turtle import *
    
    for i in range(5):
        up()
        goto(0,-20*(i+1))
        down()
        circle(20*(i+1))

    3、用while循环画太阳花

    from turtle import *
    color("red","yellow")
    begin_fill()
    while True:
        forward(200)
        left(170)
        if abs(pos())<1:
            break
    end_fill()
    done()

    4、用函数定义画五个五角星

    import turtle
    turtle.setup(600,400,0,0)
    turtle.color("yellow")
    turtle.bgcolor("red")
    turtle.fillcolor("yellow")
    
    def cyz_goto(x,y):
        turtle.up()
        turtle.goto(x,y)
        turtle.down()
    
    def cyz_draw5(r):
        turtle.begin_fill()
        for i in range(5):
            turtle.forward(r)
            turtle.right(144)
        turtle.end_fill()
    
    cyz_goto(-250,100)
    cyz_draw5(100)
    
    cyz_goto(-135,170)
    cyz_draw5(40)
    
    cyz_goto(-100,120)
    cyz_draw5(40)
    
    cyz_goto(-100,70)
    cyz_draw5(40)
    
    cyz_goto(-135,20)
    cyz_draw5(40)
    
    turtle.hideturtle()

    5、用函数定义画钻石花瓣的太阳花

    import turtle
    turtle.color("orange")
    turtle.fillcolor("blue")
    turtle.begin_fill()
    def draw_leng():
        for i in range(1,3):
            turtle.forward(100)
            turtle.right(45)
            turtle.forward(100)
            turtle.right(135)
    
    for i in range(1,37):
        draw_leng()
        turtle.right(10)
    turtle.end_fill()
    turtle.right(90)
    turtle.forward(300)

    6、输入学号,识别年级、专业、序号。

    r=input("请输入学号:")
    print("年级:",r[:4])
    print("专业:",r[8:10],"网络工程")
    print("学号:",r[10:12])

    7、输入1-7的数字,输出对应的“星期几”。

    weekstr="星期一星期二星期三星期四星期五星期六星期日"
    weekid=eval(input("请输入星期数字(1-7):"))
    pos=(weekid-1)*3
    print(weekstr[pos:pos+3])

  • 相关阅读:
    return和yield的区别
    基本装饰器
    javascript实例:两种方式实现tab栏选项卡
    javascript实例:路由的跳转
    javascript实例:点亮灯泡
    标签页QTabWidget
    主窗口QMainWindow和启动画面
    各种对话框
    列表视图QlistView
    拆分窗口QSplitter
  • 原文地址:https://www.cnblogs.com/chenyuanzhao/p/7514102.html
Copyright © 2020-2023  润新知