Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON: You will use Turtle graphics to draw a picture containing multiple sh

ID: 662689 • Letter: I

Question

IN PYTHON:

You will use Turtle graphics to draw a picture containing multiple shapes of multiple colors and arranged to be visually pleasing. Although you are free to decide the precise shape(s) and layout for your picture, some aspect of the picture must depend on a numeric value input by the user. For example, the input might determine the size of the shapes, the number of shapes, or their spacing.

Project Description / Specification: Your program must: 1. Output a brief descriptive message of what the program does. 2. Repeatedly prompt for the input until the user supplies values of the correct form (discard incorrect inputs). Your prompt should say what form of input is needed. 3. Draw a picture containing multiple shapes of multiple colors, where the input value(s) is (are) used to determine some aspect of the shapes and/or their layout.

In programming your solution, you must: 1. Use at least two repetition (while or for) statements. 2. Use at least one selection (if) statement. We show example output produced by two different programs that meet these requirements at the end of this write-up.

You may be creative and create your own program, or you may choose to mimic one of these two examples. The second example shows error checking being tested.

The first program draws squares of a fixed size; they all start at the origin, but are arranged in a circular manner by manipulating the orientation of the turtle. An example interaction:

The second program draws concentric circles of many colors. The user specifies the number of circles and the radius of the largest circle.

Explanation / Answer

#!/usr/bin/env python3

import sys
import turtle
NORM_FONT = ("Helvetica", 10)
def popupmsg(msg):
popup = tk.Tk()
popup.wm_title("!")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()

def border(t, screen_x, screen_y):
"""
Draws a border around the canvas in red.
"""
# move the turtle to the center
t.penup()
t.home()

# Moving to lower left corner of the screen
t.forward(screen_x / 2)
t.right(90)
t.forward(screen_y / 2)
t.setheading(180)   
# Drawing 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)

t.penup()
t.home()

def square(t, size, color):
"""
Drawing 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_x, screen_y = screen.screensize()
t = turtle.Turtle()

# Drawing a border around the canvas
border(t, screen_x, screen_y)
x=int(input('input x coordinate'))
y=int(input('input y coordinate'))
if int(y)==y & int(x)==x:
# Draw a set of squares with varying color.
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
t.pensize(3)# size of the pen
for i, color in enumerate(colors):# for loop till all colors exhaust
square(t, (screen_y / 2) / 10 * (i+1), color) # determining dimensions of squares

print('Hit any key to exit')
dummy = input()
else:
popupmsg()
if __name__ == '__main__':
main()