Reputation: 141
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 far I have been able to create a rectangle of 1's as shown below in a 5X5
for i in range(0, 5):
X = 0
for j in range(0, 5):
X = (X*10)+1
print(X)
I would like to be able to modify this code using a for loop to be able to create a triangle like so...
1 1 1
1 1
1
How would I go about doing this? Is there also a way that I could input n and dependant on whatever number is input the program creates a triangle of that size? for example I input a 5 and it creates a triangle like...
11111
1111
111
11
1
Ive tried various different things but i'm unable to figure it out.
Upvotes: 1
Views: 14541
Reputation: 2454
don't try to modify it. make a new one
def generateLine(size):
line = ""
for i in range(0, size):
line = line+"1"
return line
for i in range(6, 0):
print generateLine(i)
Upvotes: 0
Reputation: 473863
def triangle(c, n):
for i in xrange(n, 0, -1):
print c * i
triangle("X", 5)
prints:
XXXXX
XXXX
XXX
XX
X
Upvotes: 1