Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第1张图片

 “我喜欢turtle绘图,因为代码一点点的改动,总会带来意想不到的惊喜。

一些让我心动过的案例,分享给大家,也珍藏给自己。”

—— 初中信息技术教师  许华丽


Python Turtle绘图:24节气倒计时(1.使用列表存储数据)icon-default.png?t=M1L8http://mp.weixin.qq.com/s?__biz=MzIwMjE1MTI4MA==&mid=2247503768&idx=1&sn=66be2621615d67889741436a66a76d9e&chksm=96e18b87a19602919cd2e6fcb7d723618848296ac16e6c8dd4058f96269e183d162ea94f6bf0&scene=21#wechat_redirect

在上一期案例分享中,重点分析了“24节气倒计时”的绘制方法。参考代码中是用“列表”来存储数据的,这一期会重点讲解“字典”的创建和访问方法,并使用“字典”来优化代码。

    案例:24节气倒计时    

  难度:★★☆☆☆  

 【效果预览】 

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第2张图片

 

【代码参考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=["随风潜入夜 润物细无声","春雷响 万物长","春风如贵客 一到便繁华",
      "清明时节雨纷纷","风吹雨洗一城花","天地始交 万物并秀",
      "物至于此 小得盈满","家家麦饭美 处处菱歌长","绿筠尚含粉 圆荷始散芳",
      "荷风送香气 竹露滴清响","桂轮开子夜 萤火照空时","天阶夜色凉如水 坐看牵牛织女星",
      "春种一粒粟 秋收万颗子","露从今夜白 月是故乡明","",
      "","","",
      "","","",
      "","",""]

# 定义变量n,作为累加器
n=0

### ③ 自定义函数fun()
# fun()的功能是,写一个倒计时数字,以及一个节气的名称、英文名称和对应的诗词
def fun():
    global n                      # 强制n是全局变量的n(即声明本函数体内的变量n为主函数中的那个全局变量n)
    turtle.clear()                # 清除屏幕中的已有绘图。(如果不用clear(),5个数字会重叠在一起)
    turtle.dot(500,"#b1352b")     # 画一个直径为500,颜色为#b1352b(一种红色)的圆点,作为背景装饰
    turtle.penup()                # 海龟抬脚,移动时不会留下痕迹,但是仍然可以写字,所以后面无需再写pendown()
    turtle.setheading(-90)        # 让海龟头部向下

    turtle.backward(10)           # 海龟向上移动10像素,准备写字.(如果不向上移动10像素,海龟写出的几行字会偏下)
    turtle.write(24-n,align="center",font=("黑体",120))       # 写倒计时数字(第1行)
    turtle.forward(50)            # 海龟向下移动50像素,准备写字
    turtle.write(name[n],align="center",font=("黑体",30))     # 写节气中文名称(第2行)
    turtle.forward(50)            # 海龟再向下移动50像素,准备写字
    turtle.write(en_name[n],align="center",font=("Arial",25)) # 写节气英文名称(第3行)
    turtle.forward(50)            # 海龟再向下移动50像素,准备写字
    turtle.write(poem[n],align="center",font=("隶书",20))     # 写节气对于诗词(第4行)

    turtle.home()                 # 海龟返回原点,回到初始位置
    n=n+1                         # 累加器n加1


### ④ 主程序
# 显示两行引导文字
turtle.pencolor("black")         # 设置画笔颜色为黑色black
turtle.penup()                   # 海龟抬脚,移动时不会留下痕迹,但是仍然可以写字,所以后面无需再写pendown()
turtle.setheading(-90)           # 让海龟头部向下
turtle.backward(30)              # 海龟向上移动30像素(步),准备写字.(如果不向上移动30像素,海龟写出的两行字会偏下)
turtle.write("让我们一起倒计时,迎接春的到来",align="center",font=("黑体",20))  # 写中文引导语
turtle.forward(60)               # 海龟再向下移动60像素(步),准备写字
turtle.write("Let's greet the arrival of spring with a countdown",align="center",font=("Arial",20))  # 写英文引导语
turtle.home()                    # 海龟返回原点,回到初始位置

# 24秒倒计时开始
turtle.pencolor("white")         # 设置画笔颜色为白色white
for i in range(24):              # 每次调用fun()写4行文字,要循环24次
    # 调用自定义函数fun()
    turtle.ontimer(fun, t=1000*(i+1))  # 安装一个计时器,t毫秒后调用fun()函数

### ⑤ 海龟绘图结束,隐藏海龟
turtle.hideturtle()

 【提出问题】 

使用列表存储24节气相关数据的时候,会遇到以下问题:

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第3张图片

比如,我想检查一下“大暑”的中英文名称和古诗词书写是否正确,就要先数行,再数列来确定它们的位置,比较浪费时间。(默认读者的英文和我差不哦,不熟悉24节气单词。)

如果还想检查24节气的中英文名称和古诗词是否一一对应,那就更麻烦了。

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第4张图片

 图片来源于北京冬奥会开幕式24节气倒计时视频

每个节气的中文名称英文名称、对应的古诗词,如何在存储数据的时候就做到三者一一对应呢?

 【解决问题】 

在Python中就有专门用来表示对应关系的数据结构——字典。

字典也叫关联数组,键(key)值(value)成对组

1. 创建字典的方法

字典由大括号{}将元素括起来,每个元素包含“键”和“值”两部分,表示一种对应关系。键和值中间以冒号隔开,每个元素(键值对)之间用逗号隔开。

语法格式

{key1:value1  , key2:value2 }

举例:

以上,name叫做键(key),对应的具体节气(雨水)叫做值(value)

en_name叫做键,Rain Water是值;

poem是键,"随风潜入夜 润物细无声"是值。

现在,我们可以为每个节气创建一个字典,将每个节气的中文名称、英文名称和对应的古诗词都存储在一起,这样就不会出现错位的问题了。

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第5张图片

{"NAME":"雨水","EN_NAME":"Rain Water","POEM":"随风潜入夜 润物细无声"}

{"NAME":"惊蛰","EN_NAME":"Awakening of Insects","POEM":"春雷响 万物长"}

再给以上两个字典分别起个名字,也就是将字典赋值给一个变量。

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第6张图片

2. 访问字典元素的方法

语法格式:  字典 [  ]

字典中的每个元素表示一种对应关系,根据提供的“键”作为索引(下标)可以访问对应的“值”,如果字典中不存在这个“键”就会抛出异常。

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第7张图片

 【重难点突破】 

访问列表中元素的语法格式: 列表 [ 索引 ]

访问字典中元素的语法格式: 字典 [  ]

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第8张图片

如果列表中的元素是字典,该如何访问字典中的某一个元素呢?

语法格式:  列表 [ 索引 ] [  ]

Python Turtle绘图[难度3星]:24节气倒计时(2.使用字典存储数据)_第9张图片

 【代码参考2  · 使用字典】 

# 案例6:24节气倒计时(使用字典存储数据)

### ① 程序初始化设置
import turtle                # 导入turtle库(模块)
turtle.bgcolor("#ffff55")    # 设置背景颜色为#ffff55,一种浅黄色
turtle.speed(0)              # 设置海龟的绘图速度,参数为0时最快
turtle.delay(0)              # 设置海龟绘图的延迟时间,参数为0时,表示绘图没有延迟

### ② 变量初始化设置
# 为每个节气创建一个字典,用来存储中文名称,英文名称和对应的古诗词
# NAME是每个节气的中文名称,EN_NAME是英文名称,POEM是对应的古诗词
solar_term_24={"NAME":"雨水","EN_NAME":"Rain Water","POEM":"随风潜入夜 润物细无声"}
solar_term_23={"NAME":"惊蛰","EN_NAME":"Awakening of Insects","POEM":"春雷响 万物长"}
solar_term_22={"NAME":"春风","EN_NAME":"Spring Equinox","POEM":"春风如贵客 一到便繁华"}
solar_term_21={"NAME":"清明","EN_NAME":"Pure Brightness","POEM":"清明时节雨纷纷"}
solar_term_20={"NAME":"谷雨","EN_NAME":"Grain Rain","POEM":"风吹雨洗一城花"}
solar_term_19={"NAME":"立夏","EN_NAME":"Beginning of Summer","POEM":"天地始交 万物并秀"}
solar_term_18={"NAME":"小满","EN_NAME":"Grain Buds","POEM":"物至于此 小得盈满"}
solar_term_17={"NAME":"芒种","EN_NAME":"Grain in Ear","POEM":"家家麦饭美 处处菱歌长"}
solar_term_16={"NAME":"夏至","EN_NAME":"Summer Solstice","POEM":"绿筠尚含粉 圆荷始散芳"}
solar_term_15={"NAME":"小暑","EN_NAME":"Minor Heat","POEM":"荷风送香气 竹露滴清响"}
solar_term_14={"NAME":"大暑","EN_NAME":"Major Heat","POEM":"桂轮开子夜 萤火照空时"}
solar_term_13={"NAME":"立秋","EN_NAME":"Beginning of Autumn","POEM":"天阶夜色凉如水 坐看牵牛织女星"}
solar_term_12={"NAME":"处暑","EN_NAME":"End of Heat","POEM":"春种一粒粟 秋收万颗子"}
solar_term_11={"NAME":"白露","EN_NAME":"White Dew","POEM":"露从今夜白 月是故乡明"}
solar_term_10={"NAME":"秋风","EN_NAME":"Autumn Equinox","POEM":""}
solar_term_9={"NAME":"寒露","EN_NAME":"Cold Dew","POEM":""}
solar_term_8={"NAME":"霜降","EN_NAME":"Frost's Descent","POEM":""}
solar_term_7={"NAME":"立冬","EN_NAME":"Beginning of Winter","POEM":""}
solar_term_6={"NAME":"小雪","EN_NAME":"Minor Snow","POEM":""}
solar_term_5={"NAME":"大雪","EN_NAME":"Major Snow","POEM":""}
solar_term_4={"NAME":"冬至","EN_NAME":"Winter Solstice","POEM":""}
solar_term_3={"NAME":"小寒","EN_NAME":"Minor Cold","POEM":""}
solar_term_2={"NAME":"大寒","EN_NAME":"Major Cold","POEM":""}
solar_term_1={"NAME":"立春","EN_NAME":"Beginning of Spring","POEM":""}
# 创建一个列表,列表元素都是字典
solar_terms=[solar_term_24,solar_term_23,solar_term_22,solar_term_21,
             solar_term_20,solar_term_19,solar_term_18,solar_term_17,
             solar_term_16,solar_term_15,solar_term_14,solar_term_13,
             solar_term_12,solar_term_11,solar_term_10,solar_term_9,
             solar_term_8,solar_term_7,solar_term_6,solar_term_5,
             solar_term_4,solar_term_3,solar_term_2,solar_term_1]

# 定义变量n,作为累加器
n=0

### ③ 自定义函数fun()
# fun()的功能是,写一个倒计时数字,以及一个节气的名称、英文名称和对应的诗词
def fun():
    global n                       # 强制n是全局变量的n(即声明本函数体内的变量n为主函数中的那个全局变量n)
    turtle.clear()                 # 清除屏幕中的已有绘图。(如果不用clear(),5个数字会重叠在一起)
    turtle.dot(500,"#b1352b")      # 画一个直径为500,颜色为#b1352b(一种红色)的圆点,作为背景装饰
    turtle.penup()                 # 海龟抬脚,移动时不会留下痕迹,但是仍然可以写字,所以后面无需再写pendown()
    turtle.setheading(-90)         # 让海龟头部向下
    
    turtle.backward(10)            # 海龟向上移动10像素,准备写字.(如果不向上移动10像素,海龟写出的几行字会偏下)
    turtle.write(24-n,align="center",font=("黑体",120))        # 写倒计时数字(第1行)

    turtle.forward(50)             # 海龟向下移动50像素,准备写字
    name=solar_terms[n]["NAME"]
    turtle.write(name,align="center",font=("黑体",30))      # 写节气中文名称(第2行)

    turtle.forward(50)            # 海龟再向下移动50像素,准备写字
    en_name=solar_terms[n]["EN_NAME"]
    turtle.write(en_name,align="center",font=("Arial",25))  # 写节气英文名称(第3行)
    
    turtle.forward(50)             # 海龟再向下移动50像素,准备写字
    poem=solar_terms[n]["POEM"]
    turtle.write(poem,align="center",font=("隶书",20))      # 写节气对于诗词(第4行)

    turtle.home()                  # 海龟返回原点,回到初始位置
    n=n+1                         # 累加器n加1


### ④ 主程序
# 显示两行引导文字
turtle.pencolor("black")         # 设置画笔颜色为黑色black
turtle.penup()                   # 海龟抬脚,移动时不会留下痕迹,但是仍然可以写字,所以后面无需再写pendown()
turtle.setheading(-90)           # 让海龟头部向下
turtle.backward(30)              # 海龟向上移动30像素(步),准备写字.(如果不向上移动30像素,海龟写出的两行字会偏下)
# 写中文引导语
turtle.write("让我们一起倒计时,迎接春的到来",align="center",font=("黑体",20))  
turtle.forward(60)               # 海龟再向下移动60像素(步),准备写字
 # 写英文引导语
turtle.write("Let's greet the arrival of spring with a countdown",align="center",font=("Arial",20))   
turtle.home()                    # 海龟返回原点,回到初始位置

# 24秒倒计时开始
turtle.pencolor("white")         # 设置画笔颜色为白色white
for i in range(24):              # 每次调用fun()写4行文字,要循环24次
    # 调用自定义函数fun()
    turtle.ontimer(fun, t=1000*(i+1))  # 安装一个计时器,t毫秒后调用fun()函数

### ⑤ 海龟绘图结束,隐藏海龟
turtle.hideturtle()


如果你对列表和字典比较熟悉,也可将以上代码中的12至42行直接简化写成:

# 创建一个列表,列表元素都是字典
solar_terms=[ {"NAME":"雨水","EN_NAME":"Rain Water","POEM":"随风潜入夜 润物细无声"},
              {"NAME":"惊蛰","EN_NAME":"Awakening of Insects","POEM":"春雷响 万物长"},
              {"NAME":"春风","EN_NAME":"Spring Equinox","POEM":"春风如贵客 一到便繁华"},
              {"NAME":"清明","EN_NAME":"Pure Brightness","POEM":"清明时节雨纷纷"},
              {"NAME":"谷雨","EN_NAME":"Grain Rain","POEM":"风吹雨洗一城花"},
              {"NAME":"立夏","EN_NAME":"Beginning of Summer","POEM":"天地始交 万物并秀"},
              {"NAME":"小满","EN_NAME":"Grain Buds","POEM":"物至于此 小得盈满"},
              {"NAME":"芒种","EN_NAME":"Grain in Ear","POEM":"家家麦饭美 处处菱歌长"},
              {"NAME":"夏至","EN_NAME":"Summer Solstice","POEM":"绿筠尚含粉 圆荷始散芳"},
              {"NAME":"小暑","EN_NAME":"Minor Heat","POEM":"荷风送香气 竹露滴清响"},
              {"NAME":"大暑","EN_NAME":"Major Heat","POEM":"桂轮开子夜 萤火照空时"},
              {"NAME":"立秋","EN_NAME":"Beginning of Autumn","POEM":"天阶夜色凉如水 坐看牵牛织女星"},
              {"NAME":"处暑","EN_NAME":"End of Heat","POEM":"春种一粒粟 秋收万颗子"},
              {"NAME":"白露","EN_NAME":"White Dew","POEM":"露从今夜白 月是故乡明"},
              {"NAME":"秋风","EN_NAME":"Autumn Equinox","POEM":""},
              {"NAME":"寒露","EN_NAME":"Cold Dew","POEM":""},
              {"NAME":"霜降","EN_NAME":"Frost's Descent","POEM":""},
              {"NAME":"立冬","EN_NAME":"Beginning of Winter","POEM":""},
              {"NAME":"小雪","EN_NAME":"Minor Snow","POEM":""},
              {"NAME":"大雪","EN_NAME":"Major Snow","POEM":""},
              {"NAME":"冬至","EN_NAME":"Winter Solstice","POEM":""},
              {"NAME":"小寒","EN_NAME":"Minor Cold","POEM":""},
              {"NAME":"大寒","EN_NAME":"Major Cold","POEM":""},
              {"NAME":"立春","EN_NAME":"Beginning of Spring","POEM":""} ]

---end---

你可能感兴趣的:(Python,turtle,绘图案例,python)