我有一些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">⇒</td>
<td>2.177.115.178</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/ip</td>
<td class="cli_arrow">⇒</td>
<td>2.177.115.178</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/host</td>
<td class="cli_arrow">⇒</td>
<td>unavailable</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/ua</td>
<td class="cli_arrow">⇒</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">⇒</td>
<td>33966</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/lang</td>
<td class="cli_arrow">⇒</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">⇒</td>
<td></td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/connection</td>
<td class="cli_arrow">⇒</td>
<td></td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/encoding</td>
<td class="cli_arrow">⇒</td>
<td>gzip, deflate, br</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/mime</td>
<td class="cli_arrow">⇒</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">⇒</td>
<td></td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/via</td>
<td class="cli_arrow">⇒</td>
<td>1.1 google</td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/forwarded</td>
<td class="cli_arrow">⇒</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">⇒</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">⇒</td>
<td><info>
<ip_addr>2.177.115.178</ip_addr>
<remote_host>unavailable</remote_host>
<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</user_agent>
<port>33966</port>
<language>en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6</language>
<referer></referer>
<connection></connection>
<keep_alive></keep_alive>
<method>GET</method>
<encoding>gzip, deflate, br</encoding>
<mime>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3</mime>
<charset></charset>
<via>1.1 google</via>
<forwarded>2.177.115.178, 216.239.34.21</forwarded>
</info></td>
</tr>
<tr>
<td class="cli_command">$ curl ifconfig.me/all.json</td>
<td class="cli_arrow">⇒</td>
<td>{"ip_addr":"2.177.115.178","remote_host":"unavailable","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","port":33966,"language":"en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,de;q=0.6","method":"GET","encoding":"gzip,
deflate,
br","mime":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3","via":"1.1
google","forwarded":"2.177.115.178, 216.239.34.21"}</td>
</tr>
</table>
</div>
应该在到达右下角后立即绘制剩余的外部三角形的2个边。
答案 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控制台中仍能正常工作。