scipiguy

Python 102: Draws a rectangle and labels perimeter and are

Apr 27th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # Python 102: Draws a rectangle and labels perimeter and area
  2.  
  3. from turtle import Turtle
  4.  
  5. tina = Turtle()
  6.  
  7. def rect_area(l, w):
  8.     return l*w
  9.  
  10. def rect_perimeter(l, w):
  11.     return 2*(l + w)
  12.  
  13. def draw_rect(t_name, col, l, w):
  14.     t_name.color(col)
  15.    
  16.     t_name.penup()
  17.     t_name.goto(0, 5)
  18.     t_name.pendown()
  19.     for i in range(2):
  20.         t_name.forward(l)
  21.         t_name.rt(90)
  22.         t_name.forward(w)
  23.         t_name.rt(90)
  24.    
  25.     t_name.color("black")
  26.     t_name.penup()
  27.     t_name.goto(l/2, w/2)
  28.     t_name.pendown()
  29.     t_name.write("Area: " + str(rect_area(l, w)), align = "center")
  30.     t_name.penup()
  31.     t_name.goto(l/2, (w/2)+20)
  32.     t_name.pendown()
  33.     t_name.write("Perimeter: " + str(rect_perimeter(l, w)), align = "center")
  34.    
  35. tina.showturtle()
  36. draw_rect(tina, "blue", 100, 50)
  37. tina.hideturtle()
Advertisement
Add Comment
Please, Sign In to add comment