Python 乌龟图形使用文本文件

Python turtle graphics using text file

enter image description here我需要有关 Turtle 图形的帮助。我想让脚本读取我的文本文件,然后使用文本文件中的参数绘制图形。我想我几乎明白了,但在代码的最后部分收到了错误消息。 有人可以帮帮我吗,谢谢! :)

import turtle
turtle.pensize(4)
turtle.hideturtle()
turtle.speed(5)

with open ("tenge_eksempel_2.txt" , "r") as tekst:
lest_tekst = tekst.readlines()
Antall = 0
List_lengde = len(lest_tekst)
while List_lengde > Antall:
        if List_lengde < Antall:
        turtle.done()
    try:
        verdi = int(lest_tekst[Antall])
        if verdi < 0:
            turtle.penup()
        turtle.penup()
        turtle.right(verdi)
        Antall = Antall+1
        verdi = int(lest_tekst[Antall])
        turtle.pendown()
        if verdi < 0:
            turtle.penup()
        turtle.forward(verdi)
        Antall = Antall+1
        turtle.pendown()

turtle done

我的文本文件如下所示: 黑色的 15 200 浅灰色 150 200 等...

试试这个,看看它是否能帮助您弄清楚发生了什么。

import turtle
turtle.pensize(4)
turtle.hideturtle()
turtle.speed(5)

with open ("tenge_eksempel_2.txt" , "r") as tekst:
    temp = tekst.readlines()
lest_tekst = temp [0].split ()

Antall = 1
List_lengde = len(lest_tekst)
while List_lengde > Antall:
    verdi = int(lest_tekst[Antall])
    if verdi > 0:
        turtle.right(verdi)
        Antall = Antall+1
        verdi = int(lest_tekst[Antall])
        print (verdi)
    if verdi > 0:
        turtle.forward(verdi)
    Antall = Antall+2

turtle.done ()

tenge_eksempel_2.txt 是:

black 15 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 

编辑: 在我的系统上,我得到:

给定上面的文字:black 15 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 。 假设黑色和灰色是乌龟笔的颜色,第一个数字角度和第二个笔运动,你如何将颜色(字符串)与数字分开?我从接受的答案中尝试了上面的代码,但在 python 海龟图形中没有绘图。只是一只空空的乌龟 window.

编辑:我没有收到任何错误消息,也没有收到表明进程已完成的消息。我什至把它留给 运行 大约 15-20 分钟,以防万一它很慢。