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.

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://kidscodecs.com/resources/programming/python/

Python for Kids

https://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://kidscodecs.com/create-turtles-python/
https://kidscodecs.com/python-animation-bird-wave/

Visited 354 times, 1 visit(s) today