Skip to content Skip to sidebar Skip to footer

How To Make A Triangle Using A For Loop In Python

I'm new to programming and have been experimenting with for loops to try and figure out how to make different shapes however I have encountered a problem that I cannot solve. So fa

Solution 1:

def triangle(c, n):
    for i in xrange(n, 0, -1):
        print c * i

triangle("X", 5)

prints:

XXXXX
XXXX
XXX
XX
X

Solution 2:

don't try to modify it. make a new one

defgenerateLine(size):
    line = ""for i inrange(0, size):
        line = line+"1"return line

for i inrange(6, 0):
    print generateLine(i)

Post a Comment for "How To Make A Triangle Using A For Loop In Python"