Function that draws a circle with Turtle in Python

Page 1 of 1

7 Replies - 36736 Views - Last Post: 14 September 2011 - 12:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=247041&amp;s=cab84361ba2cbac1341ec40debd07806&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kkasp

Reputation: 0
• 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
-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
"""

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

Reputation: 452
• Posts: 798
• 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?

#3 kkasp

Reputation: 0
• 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

#4 Motoma

Reputation: 452
• Posts: 798
• 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

#5 baavgai

• Dreaming Coder

Reputation: 7166
• Posts: 14,933
• 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

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)

```

#6 kkasp

Reputation: 0
• 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.

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.

```

#7 baavgai

• Dreaming Coder

Reputation: 7166
• Posts: 14,933
• 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

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

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

drawCircle((20,20), 20)

```

#8 kkasp

Reputation: 0
• 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!