## Triangle Generator

A simple coding activity to create triangles in all shapes and sizes.

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("*")
```

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("*" * 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 <= MAX_LENGTH:
print ("*" * 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 >= MIN_LENGTH:
print ("*" * length)
length -= 1
```

#### Python for Kids

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

## Author

• Patricia Foster is a computer science student at Carleton University. In addition to working professionally as a software developer, she spends her time reading and writing.

### Also In The February 2019 Issue

The craft world and the tech world collide in this fun, hands-on activity.

A simple coding activity to create triangles in all shapes and sizes.

Five ideas to create that extra special gift for Valentine’s Day.

For twenty three years, since 1996, cars have used computers to control different parts of the car.

Celebrate Lunar New Year with your own custom-built lantern, complete with LEDs and micro:bit!

Synchronizing games with players from all around the world is no easy task.

A simple thought experiment sheds light on the dangers of AI. Can we stop the earth being buried in paperclips?

What do students really learn from robotics class? And how can we make it better?

Create a dynamic optical illusion in SketchUp.

Learn how rockets may soon be able to refuel in 0G. Next stop: Mars!

‘Files’ may be easy concepts for humans, but not for computers. What’s going on inside your operating system?

Two ways to play Minecraft with an overarching storyline. Experience the game like never before!

Helping kids fall in love with coding through Minecraft mods and Raspberry Pis.

Learn why this powerful, 40-year old language is still popular today.

A better, smoother way to direct your theatrical masterpiece.

Explore the past of cellular phone technology, and take a peek into its future.

How hackers can steal the messages you send over the internet, and how to tell if your communications are secure.

Add multimedia & more to your fantasy computer game to make it cooler than ever.

It’s not as simple as it seems. Can you solve this classic programming problem?

Ever wondered what happens when you connect to a website? Time to dive into the secrets of networking!

Links from the bottom of all the February 2019 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about science and technology for February 2019.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!