Triangle Generator

Milestoned on Flickr

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.

It’s a bit annoying to type in the full address, but if you go straight to the website’s main page (www.repl.it) they’ll ask you to create an account or log in with Google or Facebook. This is also an option, especially if you want to save your work.

On your left you’ll see your editor, where you write your code. On your right is your console, which displays the result of the code.

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

Learn More

Introduction to Python

https://www.w3schools.com/python/python_intro.asp
https://www.kidscodecs.com/resources/programming/python/

Python for Kids

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

About Loops

https://www.tutorialspoint.com/python/python_while_loop.htm
https://www.pythonforbeginners.com/control-flow-2/python-for-and-while-loops

More Python activities

https://www.kidscodecs.com/create-turtles-python/
https://www.kidscodecs.com/python-animation-bird-wave/

Also In The February 2019 Issue

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

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

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?

‘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.

A better, smoother way to direct your theatrical masterpiece.

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

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.