UserSuchAndSuch
UserSuchAndSuch

Reputation: 7

Turtle does not change when I change variable + how to avoid temp variable

I created a pentagram drawing spiral in turtle, but now when I change the variable that is supposed to change the shape (point), nothing happens. What did I do wrong?

It also calculates the distance to go forward by means of the x- and y position. But since the initial pos is 0, I created a temp variable (x) to get started. Is there a way to improve this?

x = 20
point = 3
angle = 720/point
speed(0)
limit = 200

while abs(distance(0,0)) < limit:

    penup()
    forward(x)
    right(45)
    pendown()

    xpos = xcor()
    ypos = ycor()

    for i in range(point):
        forward(20)
        right(angle)

    d = math.sqrt(xpos**2 + ypos**2)
    x = d 

Upvotes: 0

Views: 201

Answers (2)

cdlane
cdlane

Reputation: 41905

I agree with @E.Aho's assessment with what's wrong (+1) but suggest you try using 360 instead of 720 in your angle calculation. It won't give you has interesting shapes (pentagons instead of five pointed stars) but for a point value of 4 or 6, for example, it should work better:

import math
import turtle

x = 20
point = 4
angle = 360 / point
limit = 200

turtle.speed('fastest')

while turtle.distance(0, 0) < limit:

    turtle.penup()
    turtle.forward(x)
    turtle.right(45)
    turtle.pendown()

    for _ in range(point):
        turtle.forward(20)
        turtle.right(angle)

    xpos, ypos = turtle.position()

    x = math.sqrt(xpos ** 2 + ypos ** 2)

turtle.exitonclick()

enter image description here

Upvotes: 1

Adam H
Adam H

Reputation: 166

Considering that you used

from turtle import *
import math

as your imports

As for the changing shapes - it works. Only two ocassions when you cannot see the difference is using point=2 and point=4, because they result in a direct line (720/2=360° and 720/4=180°)

Also, you can get rid of variables d, xpos and ypos, because you use them just once, to create new x value, which can then be written as x = math.sqrt(xcor()**2 + ycor()**2)

Upvotes: 1

Related Questions