 # How to print a pattern which essentially is an equilateral triangle using WHILE loop?

line = ‘*’
max_length = 5

while len(line) <= max_length:
print(line)
line += “*”

What should I add to it?

Hello Revant,
i don´t know if this is the fanciest solution, but it definitly worked for me

``````max_lenght = 50                  #1
space = max_lenght * (" ")       #2
line = "*"                       #3
memory = ""                      #4
while len(line) <= max_lenght:   #5
print(space + line + memory )#6
memory = line                #7
line += "*"                  #8
space = space[:-1]           #9
``````

#1 we declare the “max_lenght variable”, it could be any number depending on the size you want
#2 we create a variable(a string multiplied by a number=max_lenght) wich would print the spaces
#3 we declare the “line” variable wich would paint the body of the triangle
#4 we declare the “memory” variable wich will be a memory space that will record the previous
value of the “line” variable before each iteration occurs.
#5 we define the loop
#6 we print the line wich will consist in a string concatenation
#7 we save the value of the “line” before step #8 occurs
#8 we add a new “*” to our “line” variable
#9 we access the “space” string and substract the last character

I hope this helps

2 Likes
``````> line = '*'
> max_length = 5
> space = max_length * " "
>
> while len(line) <= max_length:
>     print(space + line )
>     line += "*"
>     space = space[:-1]
``````

I would just want one more clarification about the
space = space[:-1], How is this function being operated?
What is the change that it brings?

Sure!!

As you may see when you create the variable` space = max_lenght * (" ")`, `being max_lenght = 5`
it will return something like this: `space = " " + " " + " " + " " + " "`
By doing this, `space = space[:-1]`, you are eliminating the last character of the string in order to replace that space by the new character `"*"`
It will work like this:

``````>   " " + " " + " " + " " + " " + "*"
>   " " + " " + " " + " " + "*" + "*"
>   " " + " " + " " + "*" + "*" + "*"
``````

and so on…

2 Likes

Thank you for explaining it in very detail.
Kudos to you…

1 Like

Just one thing more
When I create the function

space = max_length * (" ")
I do not get the result that you have mentioned.
Have you made this for an explanation? Because when I run this code then I get blank output.