How to draw a triangle in python without turtle

I was trying to draw a square using asterisks but this code was not working This is the code

def drawStar[numStars]:
    for x in range[0,numStars]:
        print["* "]

def menu[]:
    # prompting the user to pick what they want drawn
    input["Welcome to my draw shapes program "]
    print["What would you like me to draw"]
    input[" Draw a Square [1]\n Draw a Rectangle [2]\n Draw a Rectangle [2]\n Draw an Arrow Head [3]\n Exit [4]"]

def drawSquare[]:
   width = int[input["What is the width of your square "]]
   for x in range[0,width]:
        drawStar[width]
drawSquare[]

This is the output i kept getting

What is the width of your square 2
* 
* 
None
* 
* 
None

dsh

11.8k3 gold badges30 silver badges51 bronze badges

asked Jan 27, 2017 at 15:17

1

Rectangle:

m, n = 10, 10
for i in range[m]:
    for j in range[n]:
        print['*' if i in [0, n-1] or j in [0, m-1] else ' ', end='']
    print[]

Triangle:

m, n = 10, 10
for i in range[m]:
    for j in range[n]:
        print['*' if i in [j, m-1] or j == 0 else ' ', end='']
    print[]

answered Jan 27, 2017 at 15:31

MaddyMaddy

1,9704 gold badges24 silver badges57 bronze badges

1

print["* "]

The print[] function by default prints a newline character after the data you provide. That is why you see each asterisk [and space] on a separate line. The print[] function accepts a keyword argument named end to specify something else to print at the end.

You probably want something like this:

def drawStar[numStars]:
    for x in range[0,numStars]:
        print["* ", end='']
    print[]

or alternatively, you can use the feature of python that strings can be multiplied by a number:

def drawStar[numStars]:
    print[ "* " * numStars ]

answered Jan 27, 2017 at 15:37

dshdsh

11.8k3 gold badges30 silver badges51 bronze badges

Python has a simple pen drawing library called turtle. Using simple movement commands, we can draw shapes using the python turtle library. When teaching python to children, turtle is a good library to introduce to get children excited about the language and its features.

The basic actions used in the following examples are,

  • Draw a line with pen - forward[] command
  • Move without drawing - penup[], pendown[] commands
  • Turn the pen to an angle - left[], right[] commands

The following python program draws a simple equilateral triangle,

import turtle

board = turtle.Turtle[]

board.forward[100] # draw base

board.left[120]
board.forward[100]

board.left[120]
board.forward[100]

turtle.done[]

The following python program draws a right angled triangle,

import turtle

board = turtle.Turtle[]

board.forward[100] # draw base

board.left[90]
board.forward[100]

board.left[135]
board.forward[142]

turtle.done[]

The following python program draws a star shape by drawing two identical isosceles triangles,

import turtle

board = turtle.Turtle[]

# first triangle for star
board.forward[100] # draw base

board.left[120]
board.forward[100]

board.left[120]
board.forward[100]

board.penup[]
board.right[150]
board.forward[50]

# second triangle for star
board.pendown[]
board.right[90]
board.forward[100]

board.right[120]
board.forward[100]

board.right[120]
board.forward[100]

turtle.done[]

Le’ts learn how to draw a triangle in Python. We can easily do that using the turtle module.

These are the methods that we will use to create a triangle.

  • Turtle[]: It will instantiate a new turtle object.
  • forward[]: It takes a number and moves the turtle [pen] by that given distance.
  • left[]: It turns the turtle to a given angle in the anti-clockwise direction. By default, it takes an angle in degrees.
  • right[]: It turns the pen in the clockwise direction.
  • fillcolor[]: This method sets the color of the shape to be drawn.
  • begin_fill[]: We need to call this method before drawing the shape that needs to be filled.
  • end_fill[]: We need to call this method after we have drawn the shape. It fills it with the given color.

Now that we know all the required methods, let’s get started and draw a basic triangle.

import turtle

turt = turtle.Turtle[] 
#instantiate a new object

turt.fillcolor["cyan"] #set the color of the triangle to cyan

turt.begin_fill[]



turt.left[120]

turt.forward[150]

turt.left[120]

turt.forward[150]

turt.left[120]

turt.forward[150]



turt.end_fill[]

turtle.done[]#pauses the program

Initially, we create a new turtle object. Then, we set the color of the shape to cyan and call the begin_fill[] method. After that, we start drawing the triangle.

First, we rotate the pen 120o anti-clockwise, and then we move it forward 150 pixels in that direction. This is what we get.

Draw a triangle in Python Step 1

Then, we need to go down. For that, we again rotate 120o anti-clockwise and then 150 pixels forward.

Draw a triangle in Python Step 2

The last step is to draw a horizontal line to complete the triangle. We need to repeat the same step. The final triangle is given below.

Draw a triangle in Python Step 3

Instead of repeating the same lines of code, we can use a loop. Let’s see.

import turtle



turt = turtle.Turtle[] #instantiate a new object



turt.fillcolor["cyan"] #set the color of the triangle to cyan
turt.begin_fill[]



for i in range[0, 3]:

    turt.left[120]

    turt.forward[150]


turt.end_fill[]

turtle.done[]

The above triangle is equilateral. Let’s now draw a right-angled triangle.

import turtle



turt = turtle.Turtle[] #instantiate a new object



turt.fillcolor["cyan"] #set the color of the triangle to cyan

turt.begin_fill[]





turt.left[90]

turt.forward[150]

turt.left[225]

turt.forward[212.13]

turt.left[225]

turt.forward[150]



turt.end_fill[]

turtle.done[]

Output

Triangle in Python

Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!

How do you draw a triangle in Python?

How to Draw a Triangle in Python Turtle.
Draw a line with pen - forward[] command..
Move without drawing - penup[], pendown[] commands..
Turn the pen to an angle - left[], right[] commands..

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[].

How do you get rid of the turtle shape in Python?

hideturtle[] to your turtle's name or to turtle directly and it will hide the turtle. This method can be used as this Python code sample: turtle. hideturtle[]

How do you make a spiral triangle turtle in Python?

To draw something on the screen[cardboard] just move the turtle[pen]..
Import turtle and create a turtle instance..
Using for loop[i = 0 to i< n * 3] and repeat below step. turtle. forward[i * 10]. turtle. right[120]..
Close the turtle instance..

Chủ Đề