DEV Community

Cover image for Make Triangle With Python
Mert Nuhuz
Mert Nuhuz

Posted on

Make Triangle With Python

Today we are going to do a triangular practice, which is commonly used by people who are new to python. We do not need to include any library in our project. First, let's take a value from the user and make a triangle that extends by the given value. To do this, we will use the input function, a method provided by python. "input" function allows you to receive input from the user. To make a small example to see sample usage:

userChoice = int(input("Please give a number: ")) 
print("Twice the number you give: {number}".format(number=userChoice*2))
Enter fullscreen mode Exit fullscreen mode

Output:

Please give a number: 2
Twice the number you give: 4
Enter fullscreen mode Exit fullscreen mode

As you can see, we took an input from the user and output twice the output. Now that we know how to retrieve data from the user, we can continue with our first example. As I mentioned, we will take the length of the triangle that will consist of the user.

number_of_lines =  int(input("Please enter how many lines you want: "))
Enter fullscreen mode Exit fullscreen mode

The input we receive will be the number of rows of the triangle we will create. From this point on, I would like to proceed through the code, so that you can understand more easily.

for line_number in range(1,number_of_lines+1):
    print ("*" * line_number)
Enter fullscreen mode Exit fullscreen mode

Now, as I mentioned, we created our for loop, but you've noticed a detail here. Namely; The range function returns a list of up to one missing parameter from the first parameter you entered and the second parameter. For example: If you say range (1,10), you get [1,2,3,4,5,6,7,8,9]. As you may notice, it does not include the second parameter we set. Since we actively use the number of lines in the form of output that we will use, we create a list that includes the number given to us by saying "number_of_lines + 1". Then, using the "for" loop, we move the elements in the list that we have created one by one. We call each element hypothetically "line_number" to make it descriptive. After you put the sign on top of the colon, we give the output command to handle the process we want to make in the loop.

 The process written in the print method is actually like a multiplication. If you wanted to do the same process as a number again would not be a nuisance. When you call print (2 * 3), it would return 6 as output. All we do here is the process; not multiplying a number, but multiplying a text. In this way, the number of lines in each line, "*" character prints. I mean, what we want.

If we run the code we write:

Please enter how many lines you want: 5
*
**
***
****
*****
Enter fullscreen mode Exit fullscreen mode

As you can see, we have created a triangle of lines as many as we have given. Now let's make the exact opposite of the wall against which the same is leaning. For this, you should think like this.

        As you can see from the previous example, the maximum number of characters in a line is actually the number given by the user. So if 5 rows are desired, a maximum of 5 stars will come together to form the last row. What we want to do is to reverse the side of the triangle we have created. In fact, we actually put a certain amount of space in front of the stars that we normally press immediately and press it so that it is on the opposite side. In doing so, we will use the number of lines I just mentioned. For example, we wanted a 6-line star. In this case, our triangle can be a maximum of 6 characters. So if we look at the first line, it should be 6 lines long, so that it is compatible with the triangle underneath, and the main event is 6-1 = 5 lines. It's easy for us to automate this too. In the loop we just made, we'll do the multiplication that we use to print "*" in the "" (space) character:

for line_number in range(1,number_of_lines+1):
      print(" "* (number_of_lines-line_number) + "*"*line_number)
Enter fullscreen mode Exit fullscreen mode

If we look at the sample output we will get when we run:

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

Here we do what we want. The next is to create a pattern by combining these two examples. For this, first we have to do the samples together, how to print it out we have to do it. The process that we have to do can actually be done in one line. Just doubling the number of spaces we've put together when creating the opposite triangle we just made. In this case, the "print" method of multiplication by two works very well. Let's look at the code and sample output:

for line_number in range(1,number_of_lines+1):
       print("*" * line_number+" "* ((number_of_lines-line_number)*2) + "*"*line_number)
Enter fullscreen mode Exit fullscreen mode

Output:

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

That's exactly what we wanted. Now we have to turn these triangles upside down to remove the pattern I mentioned at the beginning of the article. In this case, we actually need to change the location of the parameters we have written:

for line_number in range(1,number_of_lines+1):
        print("*" * line_number+" "* ((number_of_lines-line_number)*2) + "*"*line_number)
for line_number in range(1,number_of_lines+1):
        print("*" * (number_of_lines-line_number)+" "* (line_number*2) + "*"*(number_of_lines-line_number))
Enter fullscreen mode Exit fullscreen mode

Output:

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

Now, we have to print this pattern by repeating it so that it will look like a diamond. For this, I used a repeating for loop again until the desired line.

for loop_number in range(0,number_of_lines):
    for line_number in range(1,number_of_lines+1):
            print("*" * line_number+" "* ((number_of_lines-line_number)*2) + "*"*line_number)
    for line_number in range(1,number_of_lines+1):
            print("*" * (number_of_lines-line_number)+" "* (line_number*2) + "*"*(number_of_lines-line_number))
Enter fullscreen mode Exit fullscreen mode

Our absolute output is:

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

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

As you can see, at the end of this article we started by making only a triangle, you have an application where you can print your own pattern by playing with a few numbers. I wish it was useful.

Top comments (0)