Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:将坐标从文本文件传递给Turtle_Python 3.x_Turtle Graphics - Fatal编程技术网

Python 3.x Python:将坐标从文本文件传递给Turtle

Python 3.x Python:将坐标从文本文件传递给Turtle,python-3.x,turtle-graphics,Python 3.x,Turtle Graphics,我有一个测试文件,上面有坐标,我的目标是创建一个函数,该函数将文本文件转换为坐标,以绘制海龟图像: river, 5 500, 500 -500, 360 400, 500 shadow, 4 500, 300 5, 500 300, 400 到目前为止,我有以下几点 f =open("coordinates.txt", "r") for line in f: line=line.split(",") data=[] if line: data.app

我有一个测试文件,上面有坐标,我的目标是创建一个函数,该函数将文本文件转换为坐标,以绘制海龟图像:

river, 5
500, 500
-500, 360
400, 500

shadow, 4
500, 300
5, 500
300, 400
到目前为止,我有以下几点

f =open("coordinates.txt", "r")
for line in f:
    line=line.split(",")
    data=[]
    if line:
        data.append([i.strip() for i in line])
运行后,我得到以下信息:

    [['river', '5']]
    [['500', '500']]
    [['-500', 360]]
    [['400', '500']]
    [['']]
    [['shadow', '4']]
    [['500', '300']]
    [['5', '500']]
    [['300', '400']]
    [['']]
但当我把它穿过乌龟时,它会断裂,不起作用。我的海龟功能如下:

p=[]
letter=block[0]
for line in block[1:]:
          l.append(line)
k=p[0]
turtle.setpos(k[0],k[1])
[('river', '5'), (500, 500), (-500, 360), (400, 500), ('shadow', '4'), (500, 300), (5, 500), (300, 400)]

这似乎是一个随机的代码集合,而不是一个程序。下面是我从数据和代码中重建程序的意图。数据被读取到一个列表中,如下所示:

p=[]
letter=block[0]
for line in block[1:]:
          l.append(line)
k=p[0]
turtle.setpos(k[0],k[1])
[('river', '5'), (500, 500), (-500, 360), (400, 500), ('shadow', '4'), (500, 300), (5, 500), (300, 400)]
然后用乌龟画成线

import turtle

turtle.setup(1000, 1000)

data = []

with open('coordinates.txt') as my_file:
    for line in my_file:
        line = line.rstrip()

        if line:
            x, y = line.split(', ', maxsplit=1)

            try:
                data.append((int(x), int(y)))
            except ValueError:
                data.append((x, y))

print(data)

turtle.penup()

for pair in data:
    if isinstance(pair[0], int):
        turtle.setpos(pair)
        turtle.pendown()
    else:
        print('Drawing {} ({})'.format(*pair))
        turtle.penup()

turtle.hideturtle()

turtle.done()
我不能说示例图很有趣:


块的内容是什么??您能否更具体地说明什么中断、如何中断以及什么不起作用?例如,当呈现到列表中时,哪一行引发来自文件的错误etcblock所以
等于
数据
?还是它的一部分?块等于数据