How do you make a triangle shape in python?

I want to draw the shape of a triangle using python. I have already drawn the shape of circle but I cannot draw the triangle. Could someone please help me with this?

This is my code for the circle and I want to use the same type of code for the triangle.

import graphics
import random
win=graphics.GraphWin("Exercise 7",500,500)
win.setBackground("white")
for i in range(1000):
    x=random.randint(0,500)
    y=random.randint(0,500)
    z=random.randint(1,100)
    point = graphics.Point(x,y)
    circle=graphics.Circle(point,z)
    colour=graphics.color_rgb(random.randint(0,255),
                              random.randint(0,255),
                              random.randint(0,255))
    circle.setFill(colour)
    circle.draw(win)
win.getMouse()
win.close()

Thanks!

How do you make a triangle shape in python?

Drawing with Turtle in Python is really fun. In the past handful of tutorials, we learned how to import the Turtle module for use in our programs saw how to make the turtle (pen) move on the canvas, made the turtle change directions on the canvas, saw how to use loops in turtle, and made drawings of shapes using variables. Now we will take a look at drawing another type of Polygon, the triangle using the Turtle library in Python.


Define A Triangle Function

To draw a triangle, we want to use a function, and it should make use of variables. We do this so that we can call the function many times if we like to draw many triangles of different sizes.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()

done()

How do you make a triangle shape in python?

Nice! The triangle function works. Notice that the loop uses 3 iterations shown by range(3) whereas when we drew a square it used 4 iterations in the loop. Another thing we notice is that we are passing in 120 as the degrees to turn for the left() function. Why is that? This is because when drawing a triangle, you need to use the outside angle rather than the inside angle. What we are drawing here is an equilateral triangle since all three angles of an equilateral triangle add up to 180 degrees.

How do you make a triangle shape in python?


Drawing More Triangles

Now we can use the draw_triangle() function a couple of times in combination with moving the turtle to a different spot on the canvas for a nice effect.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()
right(180)
forward(100)
right(180)
draw_triangle(200)

done()

How do you make a triangle shape in python?

The following iteration takes that idea a step further to draw three triangles in different spots on the canvas.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()
right(180)
forward(100)
right(180)
draw_triangle(200)
right(180)
forward(100)
right(180)
draw_triangle(250)

done()

How do you make a triangle shape in python?


Drawing Triangles In A Loop

Calling the draw_triangle() inside of a loop makes for some really cool effects.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


for i in range(20):
    draw_triangle()
    right(1)

done()

How do you make a triangle shape in python?

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


for i in range(40):
    draw_triangle()
    right(10)

done()

How do you make a triangle shape in python?

How do you make a triangle in Python code?

Programs to print triangles using *, numbers and characters In the first loop, we iterate from i = 0 to i = rows . The second loop runs from j = 0 to i + 1. In each iteration of this loop, we print i + 1 number of * without a new line. Here, the row number gives the number of * required to be printed on that row.

How do you print a triangle in Python?

Pattern - 4: Printing Triangle Pyramid.
n = int(input("Enter the number of rows: ")).
m = (2 * n) - 2..
for i in range(0, n):.
for j in range(0, m):.
print(end=" ").
m = m - 1 # decrementing m after each loop..
for j in range(0, i + 1):.
# printing full Triangle pyramid using stars..

How do you draw shapes in Python?

Object-oriented Programming in Python: Create Your Own Adventure Game.
from shapes import Paper, Triangle, Rectangle, Oval..
paper = Paper().
rect1 = Rectangle().
rect1. set_width(200) rect1. set_height(100) rect1. ... .
rect1. draw().
paper. display().
# put the code to create the 2nd Rectangle here paper. display().