69503

Python: Passing coordinates from text file to Turtle

Question:

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, 500 -500, 360 400, 500 shadow, 4 500, 300 5, 500 300, 400

so far I have the following

f =open("coordinates.txt", "r") for line in f: line=line.split(",") data=[] if line: data.append([i.strip() for i in line])

After running I get the following:

[['river', '5']] [['500', '500']] [['-500', 360]] [['400', '500']] [['']] [['shadow', '4']] [['500', '300']] [['5', '500']] [['300', '400']] [['']]

But when I pass it through turtle it breaks and does not work. My turtle function is as follows:

p=[] letter=block[0] for line in block[1:]: l.append(line) k=p[0] turtle.setpos(k[0],k[1])

Answer1:

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 = [] 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()

I can't say the example drawing is at all interesting:

<a href="https://i.stack.imgur.com/ZUGha.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/ZUGha.png" data-original="https://i.stack.imgur.com/ZUGha.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • Split a matrix into smaller matrices
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • Django - can't get highchart to display data
  • Ruby: Why does this way of using map throw an error?
  • How to generate an asynchronous reset verilog always blocks with chisel
  • How to align an image side by side with a heading element?
  • Messed up characters in webpages (especially social media)
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • How to save dynamically created textboxes and their values
  • findObjectsInBackgroundWithBlock block signature not correct
  • how can I compare dates in array to find the earliest one?
  • How gzip file gets stored in HDFS
  • Divide a $1 by 3 and adjusting 1 cent
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • pip in virtualenv gets ConnectTimeoutError
  • How do I shift the decimal place in Python?
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • wxPython: displaying multiple widgets in same frame
  • Installed module is empty
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Can I make an Android app that runs a web view in Chrome 39?
  • Return words with double consecutive letters
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • embed rChart in Markdown
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Is there any way to bind data to data.frame by some index?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • How to load view controller without button in storyboard?