Python Forum
Create a turtle drawing from .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a turtle drawing from .txt file
#14
I looked at your initial post and at your sample code and decided the file looks something like this:
red, 15, 200
blue, 150, 200
green, 300, 200
orange, 150, 200
black, 300, 200
red, 150, 200
blue, 300, 200
green, 150, 200
Each line in the file resulting in a turtle drawing a line. I changed some of the colors so it was easier for me to tell which line in the file matches which line in the drawing.

I wrote the simples program to draw the image. It draws a four pointed star.
import turtle

t = turtle.Turtle()

with open('test.txt') as file:
    for line in file:
        color, angle, x = line.split(',')
        t.pencolor(color.strip())
        t.right(int(angle))
        t.forward(int(x))
What I think you are asking for is some way to make this program a bit more robust. My program crashes if there are less that three values in a line.
Error:
Traceback (most recent call last): File "...", line 7, in <module> color, angle, x = line.split(',') ValueError: not enough values to unpack (expected 3, got 2)
It also crashes if the angle or distance are not integers.
Error:
Traceback (most recent call last): File "...", line 10, in <module> t.forward(int(x)) ValueError: invalid literal for int() with base 10: ' 200.1\n'
And it crashes if the file contains a color name that isn't known to Turtle.
Error:
Traceback (most recent call last): File "...", line 8, in <module> t.pencolor(color.strip()) File "C:\Program Files\Python38\lib\turtle.py", line 2252, in pencolor color = self._colorstr(args) File "C:\Program Files\Python38\lib\turtle.py", line 2696, in _colorstr return self.screen._colorstr(args) File "C:\Program Files\Python38\lib\turtle.py", line 1158, in _colorstr raise TurtleGraphicsError("bad color string: %s" % str(color)) turtle.TurtleGraphicsError: bad color string: hubbabubble
I am beginning to understand that your job is to handle these errors and maybe do something a little more graceful. You could attack that problem by verifying the strings before using them as colors or numbers, or you could us try/except to catch the errors when they occur.

Sorry about the cartesian/polar misdirection. That was heading off in a completely wrong direction.
Reply


Messages In This Thread
Create a turtle drawing from .txt file - by Noob101 - Jan-25-2021, 02:01 PM
RE: Create a turtle drawing from .txt file - by deanhystad - Jan-25-2021, 07:18 PM
RE: Create a turtle drawing from .txt file - by Log - Jan-29-2021, 12:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,323 Jan-13-2023, 01:35 PM
Last Post: perfringo
  how can i create a dictionary of dictionaries from a file Astone 2 2,344 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Drawing wheels in a turtle woehh 1 1,966 Apr-23-2020, 02:58 PM
Last Post: deanhystad
  Turtle Polygon drawing program tp_oz 3 3,265 Jul-23-2019, 01:01 PM
Last Post: ichabod801
  Turtle drawing Right Triangle Zatoichi 3 5,806 Feb-26-2018, 12:24 AM
Last Post: Zatoichi
  Unable to create csv file Sumaira 2 3,323 Feb-06-2018, 03:43 AM
Last Post: Sumaira
  Help drawing circles with turtle songminguk 3 4,577 Dec-19-2017, 08:43 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020