7 Replies - 17701 Views - Last Post: 14 September 2011 - 12:00 PM Rate Topic: -----

#1 kkasp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 11

Function that draws a circle with Turtle in Python

Posted 13 September 2011 - 02:35 PM

Hi! Any input on my code?
It isn't showing up any errors- but also no circle :(

"""
Program: 7_1.py
Author: kkasp
Function drawCircle
-Expect Turtle object, coordinates of the circle's center point and
circle's radius as arguments
-Function draws circle
-Draw circle's circumference by turning 3 degrees and moving a given
distance, 120 times.
-Calculate the distance moved with formula 2.0 * 3.14 * radius /120
"""


def drawCircle (turtle, centerpoint, radius):
    circumference = 2 * 3.14 * (radius/120)
    print "The circumference moved is", circumference
    turtle.up()
    (x,y) = centerpoint[-1]
    turtle.turn(3)
    turtle.move(120)
    turtle.down()
    
    
from turtlegraphics import Turtle
turtle=Turtle()
drawCircle(turtle, [(20,20)], 20)



Is This A Good Question/Topic? 0
  • +

Replies To: Function that draws a circle with Turtle in Python

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Function that draws a circle with Turtle in Python

Posted 13 September 2011 - 04:02 PM

What version of Python are you using?
Was This Post Helpful? 0
  • +
  • -

#3 kkasp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 11

Re: Function that draws a circle with Turtle in Python

Posted 13 September 2011 - 04:42 PM

I'm using 2.7
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Function that draws a circle with Turtle in Python

Posted 13 September 2011 - 04:50 PM

Well, Python 2.7 doesn't have a module named "turtlegraphics", so unless you post code for that module as well, I don't think we can help you.

You may want to check out the turtle module's documentation, as I think that is what you are looking for.

This post has been edited by Motoma: 13 September 2011 - 04:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,768
  • Joined: 16-October 07

Re: Function that draws a circle with Turtle in Python

Posted 13 September 2011 - 05:02 PM

The most common turtle graphics is this: http://docs.python.o...ary/turtle.html

It's unclear what you're using, but the logic still doesn't make sense.

# don't know what this is
from turtlegraphics import Turtle
# you'd normally do
# import turtle

def drawCircle (turtle, centerpoint, radius):
	circumference = 2 * 3.14 * (radius/120)
	print "The circumference moved is", circumference
	turtle.up() # so, you pen is up, nothing will be drawn
	# the -1 is confusing here
	(x,y) = centerpoint[-1]
	# turtle.turn(3) # which direction, exactly?
	
	# there is no move in standard turtle graphics
	turtle.move(120) # you move 120 steps, but the pen is up
	turtle.down() # pen is now down, nothing drawn

turtle=Turtle() # some kind of instnace
drawCircle(turtle,[(20,20)], 20)


Was This Post Helpful? 0
  • +
  • -

#6 kkasp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 11

Re: Function that draws a circle with Turtle in Python

Posted 14 September 2011 - 10:09 AM

I am going mainly off the textbook Fundamentals of Python and what I can find on the Internet. It is an online class and my Professor isn't responding to my emails (nor my other classmates)...unfortunately. I readjusted some things and also had a few questions- see below.
Thank you!

from turtle import * #I had used from turtlegraphics import Turtle because that
#was what the textbook indicated to do. I created code for Koch snowflake and ended
#up using this (above) which worked with Python 2.7 for me.

def drawCircle (turtle, centerpoint, radius):
    circumference = 2 * 3.14 * (radius/120)
    print "The circumference moved is", circumference
    (x,y) = centerpoint #I need to have the centerpoint for the circle,
    #is this correct way and then it is called below at (20,20)?
    turtle.up() #turtle up no drawing
    turtle.move(x,y) #move turtle to given centerpoint below
    turtle.setDirection(3) #turn 3 degrees
    turtle.down() #put Turtle down to draw circle
    turtle.circle(120) #draw circle
    
    
turtle=Turtle() #What exactly do you mean by instance? I am not seeing this explained in my textbook (Fundamentals of Python)
drawCircle(turtle,[(20,20)], 20) #(20,20) is meant to be the centerpoint I need to
#declare for the circle and the following 20 is the radius I need to declare.


Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,768
  • Joined: 16-October 07

Re: Function that draws a circle with Turtle in Python

Posted 14 September 2011 - 10:31 AM

I'm going to pretend you're using the standard turtle graphics, though given the methods you're calling...

from turtle import * #I had used from turtlegraphics import Turtle because that

def drawCircle (turtle, centerpoint, radius):
	circumference = 2 * 3.14 * (radius/120)
	print "The circumference moved is", circumference
	(x,y) = centerpoint #I need to have the centerpoint for the circle,
	#is this correct way and then it is called below at (20,20)?
	turtle.up() #turtle up no drawing
	
	# AttributeError: 'Turtle' object has no attribute 'move'
	# turtle.move(x,y) #move turtle to given centerpoint below
	
	# again, referencing the documentation already noted
	turtle.setpos(x,y)

	# AttributeError: 'Turtle' object has no attribute 'setDirection'
	# turtle.setDirection(3) #turn 3 degrees
	
	# turn which way, right or left???
	# again, from docs
	turtle.right(3)
	
	turtle.down() #put Turtle down to draw circle
	turtle.circle(120) #draw circle

turtle=Turtle()
# ValueError: need more than 1 value to unpack
# why the hell is this in an array?
# drawCircle(turtle,[(20,20)], 20)
drawCircle(turtle,(20,20), 20)



There, was that so hard? Read the documentation! Read the errors you get when you try to run it.

The way this should probably be written is:
import turtle

def drawCircle (centerpoint, radius):
	(x,y) = centerpoint
	turtle.up()
	turtle.setpos(x,y)
	# turtle.right(3) this is kind of pointless
	turtle.down()
	turtle.circle(radius)
	print "Radius is", radius
	print "The circumference moved is", (2.0 * 3.14 * radius)

drawCircle((20,20), 20)


Was This Post Helpful? 0
  • +
  • -

#8 kkasp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 11

Re: Function that draws a circle with Turtle in Python

Posted 14 September 2011 - 12:00 PM

thanks for the comments, this is really helpful to see the changes and what I was doing wrong.
You offer much better feedback/help then the book I have been staring at all day!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1