My Turtle python assignment: (Refection at the end)
##set up code
import turtle
repeat = (“true”)
print(‘This program is an eche sketch! (“!help” for list of all commands)’)
turtle.pencolor(“#000000”)
command_log = []
logs = 0
whitelist = [‘circle’, ‘triangle’, ‘square’, ‘#circle’, ‘#triangle’, ‘#square’, ‘!help’, ‘pendown’, ‘penup’, ‘endprogram’, ‘logs’]
##defining my shapes
def square():
for side in range(4):
turtle.pd()
turtle.forward(90)
turtle.right(90)
def circle():
for side in range(45):
turtle.pd()
turtle.forward(8)
turtle.right(8)
def triangle():
for side in range(2):
turtle.pd()
turtle.forward(90)
turtle.left(120)
turtle.right(60)
##initiating turtle
start_pos = “0,0”
tmp = “0,0”
turtle.goto(0,0)
x_start = 0
y_start = 0
##the main command list
while repeat == “true”:
tmp = start_pos
start_pos = input(“What coordinates would you like to draw to, or command?\n”)
real_logs = start_pos
##quality of life
turtle.setheading(0)
if start_pos == “endprogram”:
repeat = “false”
start_pos = tmp
if (start_pos[0:9]) == “pencolour”:
colour = (start_pos[9:16])
turtle.pencolor(colour)
turtle.fillcolor(colour)
start_pos = tmp
##removing crashes from invalid commands
if start_pos not in whitelist and start_pos[0:9] != “pencolour”:
tester = start_pos
tester = start_pos.replace(‘(‘, ”)
tester = tester.replace(‘)’, ”)
tester = tester.replace(‘,’, ”)
tester = tester.replace(‘-‘, ”)
tester_output = tester.isnumeric()
if tester_output == False:
print(“invalid command”)
start_pos = tmp
if start_pos == “!help”:
print(”'(Use “#” in front of the shapes to fill them in) “circle”, “square”, “triangle”,
“penup”, “pendown”, “pencolour#RRGGBB”, “logs” “endprogram””’)
start_pos = tmp
##shape functions
if start_pos == “square”:
square()
start_pos = tmp
if start_pos == “circle”:
circle()
start_pos = tmp
if start_pos == “triangle”:
triangle()
start_pos = tmp
##filled shape functions
if start_pos == “#square”:
turtle.begin_fill()
square()
turtle.end_fill()
start_pos = tmp
if start_pos == “#circle”:
turtle.begin_fill()
circle()
turtle.end_fill()
start_pos = tmp
if start_pos == “#triangle”:
turtle.begin_fill()
triangle()
turtle.end_fill()
start_pos = tmp
##pen up/ pen down functions
if start_pos == “penup”:
turtle.pu()
start_pos = tmp
if start_pos == “pendown”:
turtle.pd()
start_pos = tmp
##command log
command = start_pos
if real_logs != “logs”:
command_log.append(str(real_logs))
logs = int((logs) + 1)
if start_pos == “logs”:
variable = 0
while variable != logs:
print(command_log[variable])
variable = variable + 1
start_pos = tmp
##moving function
xy_split = start_pos.split(“,”)
##sets X_Initial variable
x_start_I = (xy_split[0])
x_start = (x_start_I.replace(‘(‘, ”))
##sets Y_Initial variable
y_start_I = (xy_split[1])
y_start = (y_start_I.replace(‘)’, ”))
turtle.goto(int(x_start),int(y_start))