RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1608381
Accepted
Scuffle_knife
Scuffle_knife
Asked:2025-03-09 00:05:34 +0000 UTC2025-03-09 00:05:34 +0000 UTC 2025-03-09 00:05:34 +0000 UTC

turtle 模块以及与列表和元组的交互

  • 772

是否可以向主文件添加元组列表,以便仅通过索引即可调用 turtle?

spisok = [(t.right(90), t.forward(60), t.left(90), t.forward(30), t.left(90), t.forward(60)),
(t.penup(),t.right(45),t.forward(30),t.pendown(),t.left(90), t.forward(30)...)],

而且名单公布之后呢?仅通过输入访问列表索引。并且只绘制被调用的索引。
任务本身听起来是这样的:

使用元组列表来指定 Turtle 如何绘制数字。

我想不出一个设计,让乌龟只在我要求时才画画。

python
  • 3 3 个回答
  • 63 Views

3 个回答

  • Voted
  1. Best Answer
    Amgarak
    2025-03-09T03:55:29Z2025-03-09T03:55:29Z

    让我向您展示这个想法本身,然后您可以想出一些适合您自己需要的东西。

    主要思想是,在元组中存储的不是方法调用的结果,而是方法本身(对函数的引用)以及它们的参数:

    import turtle
    
    t = turtle.Turtle()
    
    list_commands = [
        (t.forward, (100,), t.right, (90,), t.forward, (100,)),  # Набор под индексом 0, и т.д..
    ]
    
    commands = list_commands[0]
    
    for i in range(0, len(commands), 2):
        method = commands[i]    
        args = commands[i+1]   
        method(*args)
    
    turtle.done()
    

    算法:从列表中选择所需的元组>对其进行迭代>提取所需的方法及其参数>使用其参数调用该方法。

    朱镕基当然,你可以单独去思考数据结构,但是我认为主要思想应该很清楚了。

    • 2
  2. Stanislav Volodarskiy
    2025-03-09T05:35:12Z2025-03-09T05:35:12Z

    如果需要执行一系列操作,请将其格式化为函数。将几个序列收集到一个列表中。通过索引从列表中选择一个函数并执行它:

    import turtle
    
    
    def f0(t):
        t.right(90)
        t.forward(60)
        t.left(90)
        t.forward(30)
        t.left(90)
        t.forward(60)
    
    
    def f1(t):
        t.penup()
        t.right(45)
        t.forward(30)
        t.pendown()
        t.left(90)
        t.forward(30)
        
    
    spisok = [f0, f1]
    
    t = turtle.Turtle()
    while True:
        i = int(input())
        f = spisok[i]
        f(t)
    
    $ python interactive.py
    1
    1
    1
    1
    0
    0
    0
    0
    

    在此处输入图片描述

    • 1
  3. Vitalizzare
    2025-03-10T04:04:54Z2025-03-10T04:04:54Z

    我将以命令的形式提出一种替代方案,该命令可应用于模块全局命名空间中的exec形式的行。构造蓝色等边三角形的示例:"forward(100); left(90); ..."turtle

    exec("color('blue'); down(); fd(100); lt(120); fd(100); lt(120); fd(100)", globals=turtle.__dict__)
    

    在模块级别,turtle绘图函数被定义为默认类对象上同名方法的包装器Turtle。该对象被存储为有条件的私有属性Turtle._pen。在执行命令之前,必须给自定义海龟指定该属性,以便它进行绘图。建议保存原始海龟,并事先明确地初始化它(默认情况下,它None在模块级别的第一次调用任何绘图函数之前设置在那里)。也就是说,最低限度的准备工作是这样的:

    default_pen = turtle.getpen()
    turtle.Turtle._pen = user_defined_pen
    

    我没有找到任何可以改变默认海龟的功能。因此我们使用直接赋值。

    它可能是什么样子的:

    import turtle
    from functools import partial
    
    cyphers = [
        'down(); fd(50); lt(90); fd(100); lt(90); fd(50); lt(90); fd(100); up(); lt(90); fd(50); up(); fd(10)',
        'up(); lt(90); fd(50); rt(45); down(); fd(50*2**0.5); rt(135); fd(100); lt(90); up(); fd(10)',
        'up(); lt(90); fd(100); rt(90); down(); fd(50); rt(90); fd(50); rt(45); fd(50*2**0.5); lt(135); fd(50); up(); fd(10)',
        'lt(45); down(); fd(50*2**0.5); lt(135); fd(50); rt(135); fd(50*2**0.5); lt(135); fd(50); up(); lt(90); fd(100); lt(90); fd(50+10)',
        'up(); lt(90); fd(100); down(); bk(50); rt(90); fd(50); lt(90); fd(50); bk(100); up(); rt(90); fd(10)',
        'down(); fd(50); lt(90); fd(50); lt(90); fd(50); rt(90); fd(50); rt(90); fd(50); up(); rt(90); fd(100); lt(90); fd(10);',
        'up(); lt(90); fd(50); rt(90); down(); fd(50); rt(90); fd(50); rt(90); fd(50); rt(90); fd(100); rt(90); fd(50); up(); rt(90); fd(100); lt(90); fd(10);',
        'up(); lt(90); fd(100); rt(90); down(); fd(50); rt(135); fd(50*2**0.5); lt(45); fd(50); up(); lt(90); fd(50+10)',
        'down(); fd(50); lt(90); fd(100); lt(90); fd(50); lt(90); fd(100); bk(50); lt(90); fd(50); up(); rt(90); fd(50); lt(90); fd(10)',
        'down(); fd(50); lt(90); fd(100); lt(90); fd(50); lt(90); fd(50); lt(90); fd(50); up(); rt(90); fd(50); lt(90); fd(10)'
    ]
    
    cyphers_dict = dict(zip('0123456789', cyphers))
    run_commands = partial(exec, globals=turtle.__dict__)
    default_pen = turtle.getpen()   # initialize and save the link to the default pen
    red_pen = turtle.Turtle()
    red_pen.pencolor('red')
    green_pen = turtle.Turtle()
    green_pen.pencolor('green')
    
    position = {
        'default': 'up(); goto(-5*50-4*10, 0)',
        'red': 'up(); goto(-100, -100); seth(-45)',
        'green': 'up(); goto(100, 120); seth(-45); bk(4*50+3*10)'
    }
    
    def draw_number(number: str):
        for cypher in number:
            run_commands(cyphers_dict[cypher])
    
    def set_default_pen(pen):
        turtle.Turtle._pen = pen
    
    # Draw all cyphers with the default pen
    run_commands(position['default'])
    for cypher in cyphers:
        run_commands(cypher)
    
    # Draw the year with the red pen
    set_default_pen(red_pen)
    run_commands(position['red'])
    draw_number('2025')
    
    # Draw the day and month with the green pen
    set_default_pen(green_pen)
    run_commands(position['green'])
    draw_number('0903')
    
    turtle.done()
    

    绘图示例

    • 0

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5