DEV Community

Cover image for Triangular loops: How triangles got me coding again
OnyechiRiches
OnyechiRiches

Posted on • Originally published at roccs-tech.dev

Triangular loops: How triangles got me coding again

A bad plot could easily be said to be a writer’s worst enemy. I, however, would say it’s a blank page. Similarly, another set of artists face their worst nightmare: an empty first line in an IDE.

Hi, I’m Riches, and I face these problems simultaneously. I am a writer and a programmer, and believe me when I say, a blank page sucks regardless of skill. I’ve been waiting for some sort of motivation to strike and get me coding and, in return, get me blogging about coding, but nothing hit. It seems the last creatures to get that sort of motivational impact were the dinosaurs 65 million years ago. Bad jokes aside, I’m here, so my inspiration must have finally hit. So, what was it?

An interesting game idea? A beautiful website design concept? My itching to say how AI has magically improved my productivity workflow?

Well, all of that and none at the same time. Get this, it was a triangle. After 3 years of programming abstinence, a triangle was the thing that caught my attention. What the actual-

The Problem

I’d say that the only thing that knows me better than I know myself is Google’s analytics and personal recommendations on every app I visit. So, it wasn’t too shocking to see YouTube recommending coding videos after so long. Simple - I was getting interested in them again.

On the superior doom-scrolling platform, YouTube shorts (don’t come at me with your pitchforks, I just think it’s better than TikTok), I came across a video about someone challenging university students to solve a coding challenge in Python.

The task was to programmatically print out a triangle of asterisks ' * '. This seemed rather easy and complex at the same time for a newbie coder like myself. Just,

print('*')
print('**')
print('***')
print('****')
print('*****')
Enter fullscreen mode Exit fullscreen mode

Result ->

*
**
***
****
*****
Enter fullscreen mode Exit fullscreen mode

Easy, right? Well, that’s until I noticed the person in the video uses a for loop within a for loop and somehow, ends up with a square. I don’t have a fundamental understanding of loops and iterations yet, so I can’t figure out how that happened.

futureRiches = "Darn! Now I have to know what happened"

pastRiches = "Not really, you could just-"

futureRiches = "Shh, I'm working here! So, here's what I think happened with his code:"

THEIR SOLUTION (sidetrack, feel free to skip to the next section)

#The guy in the video essentially does this:
for x in range(0, 5):
    t = ""
    for y in range(0,  5):
        t = t + "*"
    print(t)
#And the terminal prints this in response:
*****
*****
*****
*****
*****
Enter fullscreen mode Exit fullscreen mode

Trying to understand their code, I re-write it like this:

for x in range(0, 5):
    for y in range(0, 5):
        print(x, "a", y)
Enter fullscreen mode Exit fullscreen mode

And, I the terminal outputs,

0 a 1
0 a 2
0 a 3
0 a 4
1 a 0
1 a 1
1 a 2
1 a 3
1 a 4
2 a 0
2 a 1
2 a 2
2 a 3
2 a 4
3 a 0
3 a 1
3 a 2
3 a 3
3 a 4
4 a 0
4 a 1
4 a 2
4 a 3
4 a 4
Enter fullscreen mode Exit fullscreen mode

From my output, I now see that each index value of the second output gets assigned to the first index of the first one, resulting in 4 asterisks per original index, like 0 for the first loop. Now that’s how they got the square shape! Makes sense. To test my theory, I do one more experiment:

for x in range(0, 2):
    for y in range(0, 2):
        for z in range(0, 2):
            print(x, "a", y, 'a', z)
Enter fullscreen mode Exit fullscreen mode

This gives me,

0 a 0 a 0
0 a 0 a 1
0 a 1 a 0
0 a 1 a 1
1 a 0 a 0
1 a 0 a 1
1 a 1 a 0
1 a 1 a 1
Enter fullscreen mode Exit fullscreen mode

Seeing how each value is assigned from the first index of the third loop to that of the second and then the first, my theory is proven correct. Now, intrigued by how said individual got that wrong, I was desperate to GET it RIGHT!

MY SOLUTION

Thus, I set off into my favorite mobile Python IDE, Pydroid, and began creating. And failing. Over and over again. Like, you would not believe the number of times I failed to get this right. I tried using functions (for some reason) then calling the function at the end of the program and then printing out the values, but then I forgot to store my variable of asterisks somewhere and my code was just a hot mess.

All I knew was that I needed 5 rows of asterisks somehow but didn’t understand how. After trying and getting frustrated, I gave up and spoke to my mom about it, and since she didn’t understand a word I was saying, she said, “You’re overthinking it. Calm down and the answer would come to you.” Is it just me, or do mothers have this supernatural power of always being right? Because I take a break and attempt the situation 30 minutes later with a clear mind and boom! My code works!

It is essentially this:

stars = ''

for star in range(5):
    stars += '*'
    print(stars)
Enter fullscreen mode Exit fullscreen mode

Look how short and easy that is!!! Yes, you could argue that I should have specified a starting point in my range like 1, but I eventually modified my code to accept input from non-technical users so if they want 5 stars they get 5 stars. Python runs through loops with indexes of 0 to the number beside the one specified in parenthesis. So, if I print out star right after for in my code block, the output would be, 0, 1, 2, 3, 4

In my opinion, if we count all those numbers, there are 5 of them. Since I still get my expected result, I could leave my code as it is, wherein at 0, stars = ‘*’and at 1, stars = ‘**’ and so forth. Alternatively, I could opt for a more 'classy' approach by writing for star in range(1,6):However, this can become more challenging to debug when allowing users to specify the stars they require, at least in the case of an interactive console experience. Hey, I wanted 5 and I got 5!

Conclusion

To sum it up, I am coding again, all thanks to a random triangle I saw on the internet. Coding is fun, exciting, and hard in its special way and it’s not something I’m ready to give up on, at least not yet. Writing, on the other hand, is a mastery of the ancient 'star' reading arts. That's just on another level.

Thank you all for reading, and I am eager to get back into the tech space. I’m 15, and although I may be considered young to join, I've been around for some time, quietly observing and learning a few things along the way. My writing still needs to be improved, and so do my coding skills, and I’m happy to grow with such a great community that is Hashnode, and the programming community in general.

If you've ever been in a similar boat, just starting with coding, or have something to share, I hear there is a lovely comment section below.

Okay, now really, how do I properly exit a terminal (article)?

Top comments (0)