Python Turtle for loop无限运行

时间:2019-05-07 07:45:11

标签: python turtle-graphics

我有一些Turtle代码已经成功运行过,然后使用Turtle IDE在Repl.it的Python中绘制了Sierpinski垫片,但是最近当我再次尝试运行它时,它成功完成了所有工作,除非它被击中了for循环,乌龟只是开始旋转,直到很久以后才执行for循环中的行。

我已经将一个print(i)添加到forloop中,并且它反复打印0,就好像它是一个无限循环一样

<div id="cli_wrap">
    <h2>Command Line Interface</h2>
    <table id="cli_table" summary="cli">
        <tr>
            <td class="cli_command">$ curl ifconfig.me</td>
            <td class="cli_arrow">&rArr;</td>
            <td>2.177.115.178</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/ip</td>
            <td class="cli_arrow">&rArr;</td>
            <td>2.177.115.178</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/host</td>
            <td class="cli_arrow">&rArr;</td>
            <td>unavailable</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/ua</td>
            <td class="cli_arrow">&rArr;</td>
            <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap
                Chromium/74.0.3729.131 Chrome/74.0.3729.131 Safari/537.36</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/port</td>
            <td class="cli_arrow">&rArr;</td>
            <td>33966</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/lang</td>
            <td class="cli_arrow">&rArr;</td>
            <td>en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/keepalive</td>
            <td class="cli_arrow">&rArr;</td>
            <td></td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/connection</td>
            <td class="cli_arrow">&rArr;</td>
            <td></td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/encoding</td>
            <td class="cli_arrow">&rArr;</td>
            <td>gzip, deflate, br</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/mime</td>
            <td class="cli_arrow">&rArr;</td>
            <td>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
            </td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/charset</td>
            <td class="cli_arrow">&rArr;</td>
            <td></td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/via</td>
            <td class="cli_arrow">&rArr;</td>
            <td>1.1 google</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/forwarded</td>
            <td class="cli_arrow">&rArr;</td>
            <td>2.177.115.178, 216.239.34.21</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/all</td>
            <td class="cli_arrow">&rArr;</td>
            <td>

                ip_addr: 2.177.115.178
                <br>

                remote_host: unavailable
                <br>

                user_agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap
                Chromium/74.0.3729.131 Chrome/74.0.3729.131 Safari/537.36
                <br>

                port: 33966
                <br>

                language: en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6
                <br>

                referer:
                <br>

                connection:
                <br>

                keep_alive:
                <br>

                method: GET
                <br>

                encoding: gzip, deflate, br
                <br>

                mime:
                text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
                <br>

                charset:
                <br>

                via: 1.1 google
                <br>

                forwarded: 2.177.115.178, 216.239.34.21
                <br>

            </td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/all.xml</td>
            <td class="cli_arrow">&rArr;</td>
            <td>&lt;info&gt;
                &lt;ip_addr&gt;2.177.115.178&lt;/ip_addr&gt;
                &lt;remote_host&gt;unavailable&lt;/remote_host&gt;
                &lt;user_agent&gt;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap
                Chromium/74.0.3729.131 Chrome/74.0.3729.131 Safari/537.36&lt;/user_agent&gt;
                &lt;port&gt;33966&lt;/port&gt;
                &lt;language&gt;en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6&lt;/language&gt;
                &lt;referer&gt;&lt;/referer&gt;
                &lt;connection&gt;&lt;/connection&gt;
                &lt;keep_alive&gt;&lt;/keep_alive&gt;
                &lt;method&gt;GET&lt;/method&gt;
                &lt;encoding&gt;gzip, deflate, br&lt;/encoding&gt;
                &lt;mime&gt;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3&lt;/mime&gt;
                &lt;charset&gt;&lt;/charset&gt;
                &lt;via&gt;1.1 google&lt;/via&gt;
                &lt;forwarded&gt;2.177.115.178, 216.239.34.21&lt;/forwarded&gt;
                &lt;/info&gt;</td>
        </tr>
        <tr>
            <td class="cli_command">$ curl ifconfig.me/all.json</td>
            <td class="cli_arrow">&rArr;</td>
            <td>{&quot;ip_addr&quot;:&quot;2.177.115.178&quot;,&quot;remote_host&quot;:&quot;unavailable&quot;,&quot;user_agent&quot;:&quot;Mozilla/5.0
                (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/74.0.3729.131
                Chrome/74.0.3729.131
                Safari/537.36&quot;,&quot;port&quot;:33966,&quot;language&quot;:&quot;en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6&quot;,&quot;method&quot;:&quot;GET&quot;,&quot;encoding&quot;:&quot;gzip,
                deflate,
                br&quot;,&quot;mime&quot;:&quot;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3&quot;,&quot;via&quot;:&quot;1.1
                google&quot;,&quot;forwarded&quot;:&quot;2.177.115.178, 216.239.34.21&quot;}</td>
        </tr>
    </table>
</div>

应该在到达右下角后立即绘制剩余的外部三角形的2个边。

1 个答案:

答案 0 :(得分:0)

对我来说,在repl.it和Python控制台中,对您的代码进行的清理运行得很好:

from turtle import Screen, Turtle

ANGLE = 120
LENGTH = 20
ITERATIONS = 4

def SGRules(character):
    if character == 'F':
        return 'F+F-F-F+F'

    return character

def processStr(string, rules):
    new_string = ""

    for character in string:
        new_string += rules(character)

    return new_string

def createLSystem(iterations, axiom, rules):
    string = ""

    for _ in range(iterations):
        string = processStr(axiom, rules)
        axiom = string

    return string

def drawLSystem(turtle, lsys, angle, length):
    for command in lsys:
        if command == 'F':
            turtle.forward(length)
        elif command == '+':
            turtle.left(angle)
        elif command == '-':
            turtle.right(angle)

yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.shape('turtle')
yertle.color('dark green', 'light green')

sg_string = createLSystem(ITERATIONS, 'F', SGRules)
print(sg_string)
drawLSystem(yertle, sg_string, ANGLE, LENGTH)

for _ in range(2):
    yertle.left(ANGLE)
    yertle.forward(LENGTH * 2 ** ITERATIONS)

screen = Screen()
screen.mainloop()

尽管我在末尾向tkinter的事件循环添加了转移以防止窗口关闭后,您的原始代码在Python控制台中仍能正常工作。