## Tiny Organ Chips

dark mode light mode Search Menu
Search

# Triangle Generator

Milestoned on Flickr

There’s something oddly satisfying about printing out cool patterns in the console. Today, let’s tackle triangles made of stars, Python-style!

### SETUP

Open up a browser and navigate to www.repl.it/languages/python3.

To display a single star, add the following line to the editor and then hit the “Run” button (the green rectangle with a tiny triangle) at the top of the screen.

```print(&quot;*&quot;)
```

Make sure you have an opening and closing bracket, and that your asterisk is surrounded by quotation marks.

To print a line of ten stars, just add ‘* 10’ inside the round brackets:

```print(&quot;*&quot; * 10)
```

### TRIANGLE TIME

How do we expand our line of stars into a triangle? Well, you could try something like this:

But that code’s long, clunky, and it has lots of potential for typos. A better solution is to use a coding structure called a while loop. The purpose of a loop is to repeat the same action over and over, saving programmers time and energy. All the code inside the while loop is run until the loop’s condition becomes false, at which point we exit the loop.

We also define some variables that make the code easy to tweak!

```length  =  1
MAX_LENGTH = 10

while length &lt;= MAX_LENGTH:
print (&quot;*&quot; * length)
length += 1
```

Copy the code into your editor and hit the run button to try it out.

### BREAKING DOWN THE CODE

Our “length” variable counts the number of stars printed in a single line. “MAX_LENGTH”, as the name hints, is the maximum length we want in our triangle. Let’s start at 10.

The condition on line 4 says that in order for the loop to run, the value of length must be smaller or equal to MAX_LENGTH. Length starts at 1, and MAX_LENGTH is 10, so we’re off to a good start. We run the loop, print some stars, and increase the value of length by 1, making it 2.

On the second run of the loop, length (2) is still smaller than MAX_LENGTH (10). On and on we go, replaying lines 5&6, increasing ‘length’ each time, until the variable has a value of 11 and the condition of the loop is no longer true.

Line 5 is the code that displays our triangle on screen. As you can see, we multiply the asterisk with a variable instead of a fixed number. Because the value of “length” is changing every time we run the loop, the number of stars printed onscreen increases at every round.

If you want to adjust the size of your triangle, you can do it by changing MAX_HEIGHT to 5, 50, even 1000! Since you’re using a loop, the program automatically implements the changes.

### INVERSE TRIANGLES

What if you wanted to print an inverse triangle, like this?

See if you can solve it by yourself! The trick is to start ‘length’ at 10, and define a minimum length instead of a maximum length. What other pieces of the code do you need to change in order to make this work?

Solution:

```length  =  5
MIN_LENGTH = 1

while length &gt;= MIN_LENGTH:
print (&quot;*&quot; * length)
length -= 1
```

#### Python for Kids

https://kidscodecs.com/python-for-kids/