In: Computer Science
You will design and implement a graphics program that draws four concentric rectangles in a window, using four different colors of your choice, meeting the following specifications.
• The size of the window is determined by the user of the program. To be specific, you should prompt the user for both the width and height of the window (in pixels), and then you should set the size of the window using these values. (Hint: use the setup() method of the window.) Make the largest colored rectangle by setting the background color of the window. The three other colored rectangles will be drawn using the draw_rectangle() function described below in Program Design.
• After the first rectangle, the size of each rectangle will be half of the size of the one before it. Using the rectangles shown in the graphic as an example, the length and width of the red rectangle are half of the length and width of the blue rectangle, respectively. Then the length and width of the yellow rectangle are half of the length and width of the red rectangle, respectively, etc.
• You should use a for loop to draw the three rectangles. Because of this, only one line of code in your program should have a call to the function draw_rectangle().
• Use meaningful variable names.
• Use horizontal and vertical white space reasonably and consistently.
• Use named constants as appropriate.
Program Design Your program is to be implemented using the function draw_rectangle(), which draws a rectangle centered in the window of a specified size and color. In order to do this, start your program with exactly the following lines of codes: import turtle import sys sys.setExecutionLimit(60000) # 60 seconds def draw_rectangle(turt, width, height, color): turt.pencolor(color) turt.fillcolor(color) turt.up() x = - width / 2 y = height / 2 turt.goto(x,y) turt.down() turt.begin_fill() # Draw the top turt.setheading(0) turt.forward(width) # Draw the right side turt.right(90) turt.forward(height) # Draw the bottom turt.right(90) turt.forward(width) # Draw the left side turt.right(90) turt.forward(height) turt.end_fill() # Your code goes here
Please explain every step clearly and thoroughly in python, my python code keeps giving an error message.
Required program is given below:
import turtle
import sys
turt=turtle.Turtle()
def draw_rectangle(turt, width, height, color):
turt.pencolor(color)
turt.fillcolor(color)
turt.up()
x = -width / 2
y = height / 2
turt.goto(x,y)
turt.down()
turt.begin_fill() # Draw the top
turt.setheading(0)
turt.forward(width) # Draw the right side
turt.right(90)
turt.forward(height) # Draw the bottom
turt.right(90)
turt.forward(width) # Draw the left side
turt.right(90)
turt.forward(height)
turt.end_fill()
sc=turtle.Screen()
print("enter width : ")
width=int(input())
print("enter hight : ")
hight=int(input())
sc.setup(width,hight)#it will set the screen size according to user
demand
l=['red','blue','yellow','green']
for color in l:
draw_rectangle(turt,width,hight,color)
width=width/2
hight=hight/2
sc.exitonclick() #it will exit the window when we click on
screen
NOTE: In above program when I used "sys.setExecutionLimit(60000)" then it show error because may be my python version not supporting it so to close the program I used "sc.exitonclick()" in my program if your interpreter supports "sys.setExecutionLimit(60000)" than you can use it.
Above program interpreter image is given below:
Screenshots of output of above program is given below:
case1
case2
PLEASE UPVOTE THE ANSWER AND FEEL FREE TO ASK YOUR DOUBTS IN COMMENT SECTION