Question: I need to make a triangle of triangle pattern of * depending on the integer input. For example: n = 2  * *** * * * *********  n = 3  * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** *************************  I've already figured out the code for a single triangle, but I don't know how to duplicate them so they'll appear like a triangle of triangles. Here's my code for one triangle: rows = int(input()) for i in range(rows): for j in range(i, rows): print(" ", end="") for j in range(i): print("*", end="") for j in range(i + 1): print("*", end="") print() 

Answer: Code I have simplified the following code so it should now look more clear easy to understand than it used to be. n = int(input("n = ")) rows = n ** 2 base = n * 2 - 1 for row in range(rows): triangle = row // n level = row % n a_space = " " * (n - triangle - 1) * base b_space = " " * (n - level - 1) line = (a_space + (b_space + "*" * (level * 2 + 1) + b_space) * (triangle * 2 + 1)).rstrip() print(line)   How it works This approach prints out everything in one pass. In the following explanation, I will break down the components of the code, what these variables mean, and how they work. The height The first variable that I should mention is n, the number of levels, or the height of every triangle. It is also the number of triangles stacked on top of each other. Rows The second variable is rows, the number of rows in the output. We can see a pattern that the number of rows is equal to n squared.  Base The next variable is base. It is the number of asterisks at the bottom level of the triangle. It also follows a pattern of odd numbers as we may have noticed.  After that, our loop begins, iterates through every row, and prints out the result. Triangle and level These variables tell us which triangle and level of the triangle we are currently at. You can try it yourself by printing out triangle and level every iteration. for row in range(rows): triangle = row // n level = row % n print(triangle, level)   The two types of space The next part is the indentation. Just to give you a brief idea of what a_space and b_space are, here is a visual representation that describes the spaces.  a_space shrinks after every triangle, and b_space shrinks after every level. a_space = " " * (n - triangle - 1) * base b_space = " " * (n - level - 1)  We multiply a_space by base because one a_space has a length equal to the base. Printing out the line Let's look at it step-by-step.   First, we start our line with a_space.  a_space   Then, we print out the asterisks. The number of asterisks follows the pattern of odd numbers.  "*" * (level * 2 + 1)   We open and close the asterisks with b_space.  b_space + "*" * (level * 2 + 1) + b_space   Then, we simply multiply the whole thing by the number of triangles stacked next to each other horizontally.  (b_space + "*" * (level * 2 + 1) + b_space) * (triangle * 2 + 1)   Putting everything together, we get the line, right-strip it, and print it out. line = (a_space + (b_space + "*" * (level * 2 + 1) + b_space) * (triangle * 2 + 1)).rstrip() print(line)   Output Let's try it out with a few test cases. n = 1 *  n = 2  * *** * * * *********  n = 3  * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** *************************  n = 4  * *** ***** ******* * * * *** *** *** ***** ***** ***** ********************* * * * * * *** *** *** *** *** ***** ***** ***** ***** ***** *********************************** * * * * * * * *** *** *** *** *** *** *** ***** ***** ***** ***** ***** ***** ***** *************************************************