More coding stuff

Been working to enhance my nested loops code using turtle graphics

#basic nested loop using turtle graphics and python
#added input checking
import turtle
import time
 
#set file name
fname= raw_input("Filename (must include .eps ")
#fname="dial.eps"
 
sides = raw_input("number of sides to shape ")
i = sides.isdigit()
while i != True:
	print("Input MUST be a number")
	sides = raw_input("number of sides to shape ")
	i = sides.isdigit()
 
sides = int(sides)
shapeangle = 360 / sides
 
for x in range(0,72):
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(150)
		turtle.left(shapeangle) 
time.sleep(1)
 
ts = turtle.getscreen()
ts.getcanvas().postscript(file=fname)
 
print "Saved image to: ", fname
print "All done. Click image to exit."
 
turtle.exitonclick()

Looking at the above code the section with the code below checks the input and makes sure it is numerical.  Before carrying on.

sides = raw_input("number of sides to shape ")
i = sides.isdigit()
while i != True:
	print("Input MUST be a number")
	sides = raw_input("number of sides to shape ")
	i = sides.isdigit()

The code in turtleloop4.3.py seems to be working now, as it draws square and fills it in  as well as a few repeats, which as I explain below is more experimentation.

import time
import turtle
import math
 
#set file name
fname= raw_input("Filename (must include .eps ")
#fname="dial.eps"
 
sides = raw_input("number of sides to shape ")
sides = int(sides)
shapeangle = 360 / sides
 
print shapeangle
print turtle.pencolor()
print turtle.fillcolor()
print turtle.pensize()
 
turtle.pen(fillcolor="Red", pencolor="blue", pensize=1)
 
for x in range(25,50):
	turtle.fill(True)
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(x)
		turtle.left(shapeangle) 
		turtle.fill(1)
turtle.forward(50)
for x in range(25,50):
	turtle.fill(True)
	turtle.left(5)
	for n in range(0,sides):
		turtle.backward(x)
		turtle.left(shapeangle) 
		turtle.fill(1)
 
#hide turtle once drawing complete
turtle.hideturtle()
#pause
time.sleep(1)
 
#grab screen
ts = turtle.getscreen()
#save canvas to filename specified earlier
ts.getcanvas().postscript(file=fname)
 
#tell user that file is saved to the specified filename
print "Saved image to: ", fname
#prompt user to click image to exit
print "All done. Click image to exit."
 
turtle.exitonclick()

version 4.4 (below) now does things a little different in that it draws a square 25×25 pixels, then rotates then draws the same square slightly bigger, 26×26 with the size being bound to the iteration number of the loop so it goes from 25×25 pixels to 50×50 pixels.  The above code repeats the same loop again in a different place (just a experiment by me here) (ver 4.3 above)

change turtle.fill (True) will fill the square in,  (False) won’t)

turtle.loop4.4.py

import time
import turtle
import math
 
#set file name
fname= raw_input("Filename (must include .eps ")
#fname="dial.eps"
 
sides = raw_input("number of sides to shape ")
i = sides.isdigit()
while i != True:
	print("Input MUST be a number")
	sides = raw_input("number of sides to shape ")
	i = sides.isdigit()
 
sides = int(sides)
shapeangle = 360 / sides
 
#these lines are for diagnostics
 
#print shapeangle
#print turtle.pencolor()
#print turtle.fillcolor()
#print turtle.pensize()
 
#turtle.pen(fillcolor="Red", pencolor="blue", pensize=1)
 
for x in range(25,50):
	turtle.fill(False)
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(x)
		turtle.left(shapeangle) 
 
#hide turtle once drawing complete
turtle.hideturtle()
#pause
time.sleep(1)
 
#grab screen
ts = turtle.getscreen()
#save canvas to filename specified earlier
ts.getcanvas().postscript(file=fname)
 
#tell user that file is saved to the specified filename
print "Saved image to: ", fname
#prompt user to click image to exit
print "All done. Click image to exit."
 
turtle.exitonclick()

So i have modified the code a little,  will upload to git hub at some point but the modifications are easy enough to add anyway.

To avoid confusion as some of the above code is a bit mix and match.

for x in range(0,72):
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(150)
		turtle.left(shapeangle)

 

Draws your basic loop,   where shape angle is the number you enter at the start OR can be entered here as the angle required for your shape (Triangle 120 degrees, Square 90 degrees)

for x in range(25,50):
	turtle.fill(False)
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(x)
		turtle.left(shapeangle)

Is similar only it makes the square bigger each time,   hence the loop range is (25,50)

The Lines

sides = raw_input("number of sides to shape ")
i = sides.isdigit()
while i != True:
	print("Input MUST be a number")
	sides = raw_input("number of sides to shape ")
	i = sides.isdigit()

Are responsible for taking input and checking it is valid and either returning an error or running the loop  accordingly.