赞
踩
“我喜欢turtle绘图,因为代码一点点的改动,总会带来意想不到的惊喜。
一些让我心动过的案例,分享给大家,也珍藏给自己。”
—— 初中信息技术教师 许华丽
在上一期案例分享中,重点分析了“24节气倒计时”的绘制方法。参考代码中是用“列表”来存储数据的,这一期会重点讲解“字典”的创建和访问方法,并使用“字典”来优化代码。
案例:24节气倒计时
难度:★★★☆☆☆
【效果预览】
【代码参考1 · 使用列表】
下面就是上一期分享的参考代码。使用列表来存储数据,共创建了3个列表。
- # 案例5:24节气倒计时(使用列表存储数据)
-
- ### ① 程序初始化设置
- import turtle # 导入turtle库(模块)
- turtle.bgcolor("#ffff55") # 设置背景颜色为#ffff55,一种浅黄色
- turtle.speed(0) # 设置海龟的绘图速度,参数为0时最快
- turtle.delay(0) # 设置海龟绘图的延迟时间,参数为0时,表示绘图没有延迟
-
- ### ② 变量初始化设置
- # 创建列表name,存放24节气中文名称
- name=["雨水","惊蛰","春风",
- "清明","谷雨","立夏",
- "小满","芒种","夏至",
- "小暑","大暑","立秋",
- "处暑","白露","秋风",
- "寒露","霜降","立冬",
- "小雪","大雪","冬至",
- "小寒","大寒","立春"]
-
- # 创建列表en_name,存放24节气英文名称
- en_name=["Rain Water","Awakening of Insects","Spring Equinox",
- "Pure Brightness","Grain Rain","Beginning of Summer",
- "Grain Buds","Grain in Ear","Summer Solstice",
- "Minor Heat","Major Heat","Beginning of Autumn",
- "End of Heat","White Dew","Autumn Equinox",
- "Cold Dew","Frost's Descent","Beginning of Winter",
- "Minor Snow","Major Snow","Winter Solstice",
- "Minor Cold","Major Cold","Beginning of Spring"]
-
- # 创建列表poem,存放诗词
- # 没有对应的诗词,就定义为空字符串。如果不定义,调用时会报错
- poem=["随风潜入夜 润物细无声","春雷响 万物长","春风如贵客 一到便繁华",
- "清明时节雨纷纷","风吹雨洗一城花","天地始交 万物并秀",
- "物至于此 小得盈满","家家麦饭美 处处菱歌长","绿筠尚含粉 圆荷始散芳",
- "荷风送香气 竹露滴清响","桂轮开子夜 萤火照空时","天阶夜色凉如水 坐看牵牛织女星",
- "春种一粒粟 秋收万颗子","露从今夜
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。