Python Forum
Print triangle using while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Print triangle using while loop (/thread-22950.html)



Print triangle using while loop - tuxandrew - Dec-04-2019

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()



RE: Print triangle using while loop - ichabod801 - Dec-04-2019

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.


RE: Print triangle using while loop - sumana - Dec-05-2019

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 * * * * * * * * * * * * * * * * * * * * * * * * * * * *



RE: Print triangle using while loop - micseydel - Dec-05-2019

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.