Python: Passing Coordinates From Text File To Turtle
I have a test file that has coordinates on it my aim it to create a function that takes the text file and turns it into coordinates to plot in turtle to draw an image: river, 5 500
Solution 1:
This seems to be a random collection of code rather than a program. Below is my reconstruction of the intent of the program from the data and code. The data gets read into a list as follows:
[('river', '5'), (500, 500), (-500, 360), (400, 500), ('shadow', '4'), (500, 300), (5, 500), (300, 400)]
And then drawn as lines with turtle.
import turtle
turtle.setup(1000, 1000)
data = []
withopen('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:
ifisinstance(pair[0], int):
turtle.setpos(pair)
turtle.pendown()
else:
print('Drawing {} ({})'.format(*pair))
turtle.penup()
turtle.hideturtle()
turtle.done()
I can't say the example drawing is at all interesting:
Post a Comment for "Python: Passing Coordinates From Text File To Turtle"