Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle python graphics
#1
Hello,

I need help, i need to write a function using turtle in pyhon.
That takes in entry(x0,y0,D)
x0 et y0 are the first coordinate points in this case they are (x0=10, y0=20)
and D is a list of all the coordinate points this function needs to trace.

D = [{'X': -15, 'Y': 29}, {'X': -22, 'Y': 13}, {'X': -18, 'Y': -2}, {'X': -19, 'Y': -7}, {'X': -17, 'Y': -17}, {'X': -7, 'Y': -15}, {'X': -4, 'Y': -29}, {'X': 6, 'Y': -21}, {'X': 20, 'Y': -17}, {'X': 37, 'Y': -27}, {'X': 45, 'Y': -26}, {'X': 60, 'Y': 48}, {'X': 18, 'Y': 29}, {'X': 11, 'Y': 30}, {'X': -2, 'Y': 30}, {'X': -22, 'Y': 24}, {'X': -22, 'Y': 4}, {'X': -22, 'Y': 2}, {'X': -20, 'Y': -10}, {'X': -8, 'Y': -33}]

from turtle import *


def draw_figure(x0,y0,D):
    for i in list(range(len(D))):
        forward(D.values())
        left(D.values())

print(draw_figure(10,20,D))
my program doesn't work :
Error:
'list' object has no attribute 'values'
i have tried to use only:
for i in list(range(len(D))):
forward('X')
left('Y')
But it still doesn't works.


thank you
Reply
#2
I have this working (looks like an erratic line) but i am unsure that I understand what you need.
1) Is putting the points in a dictionary essential to the setup?
(I put them simply in a 2D list)
2) Turtle moves forward with the X value and left with the Y value?
Or do you want it to jump from x,y to x,y, that's something else.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Please see [Basic] Never use "for i in range(len(sequence)):"
Reply
#4
(Jun-09-2020, 03:44 PM)DPaul Wrote: I have this working (looks like an erratic line) but i am unsure that I understand what you need.
1) Is putting the points in a dictionary essential to the setup?
(I put them simply in a 2D list)
2) Turtle moves forward with the X value and left with the Y value?
Or do you want it to jump from x,y to x,y, that's something else.

Paul

1) yes the poinds needs to be put in a dictionnary, we don't have the right to modify it.
2) this dictionnary assembles all the displacement the turtle will do; in the end we need to have a heart.

(Jun-09-2020, 03:44 PM)DPaul Wrote: I have this working (looks like an erratic line) but i am unsure that I understand what you need.
1) Is putting the points in a dictionary essential to the setup?
(I put them simply in a 2D list)
2) Turtle moves forward with the X value and left with the Y value?
Or do you want it to jump from x,y to x,y, that's something else.

Paul

1) yes the poinds needs to be put in a dictionnary, we don't have the right to modify it.
2) this dictionnary assembles all the displacement the turtle will do; in the end we need to have a heart.

I've edited my code:

def draw_figure(x0,y0,D):
    tracer(True)
    penup()
    goto(x0,y0)
    pendown()

begin_fill()
for i in D:
        pen.right('X') #to access the index of X
        pen.left('Y')
end_fill()

print(draw_figure(10,20,D1))
Error:
for i in D: NameError: name 'D' is not defined

(Jun-09-2020, 03:44 PM)DPaul Wrote: I have this working (looks like an erratic line) but i am unsure that I understand what you need.
1) Is putting the points in a dictionary essential to the setup?
(I put them simply in a 2D list)
2) Turtle moves forward with the X value and left with the Y value?
Or do you want it to jump from x,y to x,y, that's something else.

Paul

1) yes the poinds needs to be put in a dictionnary, we don't have the right to modify it.
2) this dictionnary assembles all the displacement the turtle will do; in the end we need to have a heart.

I've edited my code:

def draw_figure(x0,y0,D):
    trace(True)
    penup()
    goto(x0,y0)
    pendown()

begin_fill()
for i in D:
        pen.right('X') #to access the index of X
        pen.left('Y')
end_fill()

print(draw_figure(10,20,D1))
Error:
for i in D: NameError: name 'D' is not defined
Reply
#5
Are you sure the coordinates draw a heart ? :-)
It works , but i get the start of a heart +/- ...
What i did:
D is now a list of dictionaries.
"For coord in D:" is what Yoriz says.
Each coord has 2 dictionary keys ['X'] and ['Y'], that give simple access to the values.
After that, piece of cake.
Also, do not include D in your def call, as D is visible from within the def.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#6
(Jun-10-2020, 09:30 AM)DPaul Wrote: Are you sure the coordinates draw a heart ? :-)
It works , but i get the start of a heart +/- ...
What i did:
D is now a list of dictionaries.
"For coord in D:" is what Yoriz says.
Each coord has 2 dictionary keys ['X'] and ['Y'], that give simple access to the values.
After that, piece of cake.
Also, do not include D in your def call, as D is visible from within the def.
Paul
yes it needs to draw an ugly heart ; maybe i didn't give it entirely ;
D1 = [{'X': -15, 'Y': 29}, {'X': -22, 'Y': 13}, {'X': -18, 'Y': -2}, {'X': -19, 'Y': -7}, {'X': -17, 'Y': -17}, {'X': -7, 'Y': -15}, {'X': -4, 'Y': -29}, {'X': 6, 'Y': -21}, {'X': 20, 'Y': -17}, {'X': 37, 'Y': -27}, {'X': 45, 'Y': -26}, {'X': 60, 'Y': 48}, {'X': 18, 'Y': 29}, {'X': 11, 'Y': 30}, {'X': -2, 'Y': 30}, {'X': -22, 'Y': 24}, {'X': -22, 'Y': 4}, {'X': -22, 'Y': 2}, {'X': -20, 'Y': -10}, {'X': -8, 'Y': -33}]
it still doesn't work
def draw_figure(x0,y0):
    tracer(True)
    penup()
    goto(x0,y0)
    pendown()

begin_fill()
for coord in D:
        pen.right('X') #to access the index of X
        pen.left('Y')
end_fill()

print(draw_figure(10,20,D1))
Reply
#7
Try this, and perhaps add the filling etc later:
from turtle import *
  
def draw_figure(x0,y0):
    goto(x0,y0)
    for coord in D:
       forward(coord['X'])
       left(coord['Y'])

print(draw_figure(10,20))
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Graphics - Screen & Window Sizing jerryf 1 810 Feb-09-2023, 08:02 PM
Last Post: jerryf
  Python graphics kaltenherz 1 1,710 Sep-05-2021, 05:19 PM
Last Post: jefsummers
  Python 3 Turtle - Screen Events and Coords peteralien 0 1,678 Aug-18-2020, 11:25 PM
Last Post: peteralien
  For loops help, using python turtle SemiBeginnerPY 2 3,934 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY
  Python Turtle Help codinghero 1 15,291 Jan-04-2020, 12:46 AM
Last Post: ichabod801
  Python Graphics Help With Entries BadenJaden 1 50,342 Jul-24-2019, 10:36 PM
Last Post: Yoriz
  Python Turtle and order of implementation query Parsleigh 2 2,750 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,165 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,603 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  from graphics import * jmaloney413 2 5,192 Oct-18-2018, 08:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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