Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python 102: Draws a rectangle and labels perimeter and area
- from turtle import Turtle
- tina = Turtle()
- def rect_area(l, w):
- return l*w
- def rect_perimeter(l, w):
- return 2*(l + w)
- def draw_rect(t_name, col, l, w):
- t_name.color(col)
- t_name.penup()
- t_name.goto(0, 5)
- t_name.pendown()
- for i in range(2):
- t_name.forward(l)
- t_name.rt(90)
- t_name.forward(w)
- t_name.rt(90)
- t_name.color("black")
- t_name.penup()
- t_name.goto(l/2, w/2)
- t_name.pendown()
- t_name.write("Area: " + str(rect_area(l, w)), align = "center")
- t_name.penup()
- t_name.goto(l/2, (w/2)+20)
- t_name.pendown()
- t_name.write("Perimeter: " + str(rect_perimeter(l, w)), align = "center")
- tina.showturtle()
- draw_rect(tina, "blue", 100, 50)
- tina.hideturtle()
Advertisement
Add Comment
Please, Sign In to add comment