PYTHON TURTLE GRAPHICS write a non pure function procedure
PYTHON & TURTLE GRAPHICS
write a non pure function (procedure) named drawstar that takes no arguments and draws a 5 pointed star using python turtle graphics : the drawsar procedure should do the following : open a turtle dialogue box and have the user enter the length of each side. draw a five pointed star with each side length as entered by the user. (hint: the internal angle of each points is 36 degrees) to be honest, i really want to know how to do this on my own so if anybody could guide me it would be great. I am confused on how to assign the side length to the user input box, which is the turtle.numinput() command i believe. PLEASE help and please try to explain..
Solution
import sys
import turtle
def border(t, screen_x, screen_y):
\"\"\"(Turtle, int, int)
Draws a border around the canvas in red.
\"\"\"
# Lift the pen and move the turtle to the center.
t.penup()
t.home()
# Move to lower left corner of the screen; leaves the turtle
# facing west.
t.forward(screen_x / 2)
t.right(90)
t.forward(screen_y / 2)
t.setheading(180) # t.right(90) would also work.
# Draw the border
t.pencolor(\'red\')
t.pendown()
t.pensize(10)
for distance in (screen_x, screen_y, screen_x, screen_y):
t.forward(distance)
t.right(90)
# Raise the pen and move the turtle home again; it\'s a good idea
# to leave the turtle in a known state.
t.penup()
t.home()
def square(t, size, color):
\"\"\"(Turtle, int, str)
Draw a square of the chosen colour and size.
\"\"\"
t.pencolor(color)
t.pendown()
for i in range(4):
t.forward(size)
t.right(90)
def main():
# Create screen and turtle.
screen = turtle.Screen()
screen.title(\'Square Demo\')
screen_x, screen_y = screen.screensize()
t = turtle.Turtle()
# Uncomment to draw the graphics as quickly as possible.
##t.speed(0)
# Draw a border around the canvas
border(t, screen_x, screen_y)
# Draw a set of nested squares, varying the color.
# The squares are 10%, 20%, etc. of half the size of the canvas.
colors = [\'red\', \'orange\', \'yellow\', \'green\', \'blue\', \'violet\']
t.pensize(3)
for i, color in enumerate(colors):
square(t, (screen_y / 2) / 10 * (i+1), color)
print(\'Hit any key to exit\')
dummy = input()
if __name__ == \'__main__\':
main()

