Python Forum

Full Version: Print triangle using while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following code will print a triangle of stars.
How we can obtain the same result using while loop? And explain how it works?
Thanks in advance for any help?

l=int(input("Enter the limit:"))
for i in range(1,l+1):
    for j in range(i):
        print("*",end="")
print()
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
print("Print full Triangle pyramid using stars ")
size = 7
m = (2 * size) - 2
for i in range(0, size):
    for j in range(0, m):
        print(end=" ")
    m = m - 1 # decrementing m after each loop
    for j in range(0, i + 1):
        # printing full Triangle pyramid using stars
        print("* ", end=' ')
    print(" ")
Output:
Print full Triangle pyramid using stars * * * * * * * * * * * * * * * * * * * * * * * * * * * *
sumana, you seem to have a pattern of providing low-quality answers to posts that haven't necessarily show the effort that you should be giving them as much as you are. This is made worse by things like (1) your code doesn't fulfill the OP's requirement of while loops, so isn't useful and (2) you didn't improve upon the OP's original code, you overcomplicated it.

Please be more mindful, or we may be forced to issue formal warnings and escalate from there.