7 Replies - 30714 Views - Last Post: 24 December 2014 - 02:49 PM Rate Topic: -----

#1 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Python 3: Turtle drawing a star

Posted 24 December 2014 - 10:47 AM

I'm trying to draw a star and I'm told that each angle is 36 degrees.

So here is my code

import turtle

turtle.penup()
turtle.goto(0,0)
turtle.pendown()
i = 0
while i <= 5:
    turtle.forward(100)
    turtle.left(36)
    i+=1
turtle.done()



but it draws half of a 12 sided polygon. If I increase the degrees to something like 150 it starts looking more like a star but it doesn't join up with the starting position.

Posted Image

Posted Image

What am I doing wrong? The star in the book has 5 sides and it says each angle is 36 degrees but that can't be possible? They look more like 150 degrees.

This post has been edited by The Chief: 24 December 2014 - 10:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Python 3: Turtle drawing a star

#2 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,913
  • Joined: 12-December 12

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 10:54 AM

I demonstrate drawing stars in my tutorial. Essentially, you turn 144 degrees, not 36.

Attached Image

This post has been edited by andrewsw: 24 December 2014 - 10:59 AM

Was This Post Helpful? 3
  • +
  • -

#3 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 10:59 AM

I knew it was just a case of guessing the correct angle. although one problem, the star is upside down, it should actually look like this

Posted Image

but mine is upside down, I think it's to do with my starting position?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,913
  • Joined: 12-December 12

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 11:02 AM

Quote

I knew it was just a case of guessing the correct angle.

Not guessing I hope ;)

Quote

I think it's to do with my starting position?

It is either

  • the starting position
  • the direction the turtle is facing,
  • whether he should move first, or turn

Was This Post Helpful? 1
  • +
  • -

#5 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 11:10 AM

I changed it to turn first and made the turtle turn right instead of left.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 The Chief   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 12:08 PM

Also do you know how to make the turtle type the contents of a variable?

for example
import turtle
x1,y1,x2,y2 = eval(input("Type in some coordinates, e.g (50,80,-10,40)"))

turtle.penup()
turtle.goto(x1,y1)
turtle.write("p1", x1,y1)
turtle.pendown()
turtle.goto(x2,y2)
turtle.write("p2", x2,y2)
turtle.done()



I get the error
Traceback (most recent call last):
  File "D:/Python Projects/Basics/DrawLine.py", line 6, in <module>
    turtle.write("p1", x1,y1)
  File "<string>", line 1, in write
  File "D:\Python Interpreter\lib\turtle.py", line 3431, in write
    end = self._write(str(arg), align.lower(), font)
AttributeError: 'int' object has no attribute 'lower'



so I'm assuming turtle.write can only write strings?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,913
  • Joined: 12-December 12

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 12:25 PM

Why guessing and assuming? If you want to use the write() method then look it up in the docs or other reference material and see what arguments it uses and requires.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7262
  • View blog
  • Posts: 15,142
  • Joined: 16-October 07

Re: Python 3: Turtle drawing a star

Posted 24 December 2014 - 02:49 PM

Two things. One, your while goes six, not five, steps. Why use a while? Two, you can make the turtle do the angle math if you're not up to braining. e.g.
turtle.pendown()

# i = 0
# while i <= 5:
for i in range(5): # this actually goes 5 times, not 6
    turtle.forward(100)
    turtle.left(180) # point to your butt first, now it will work
    turtle.left(36)
    # i+=1
turtle.done()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1