1

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.

1
  • How about for j in range(0, 5-i):? Commented Aug 6, 2013 at 8:55

2 Answers 2

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

triangle("X", 5)

prints:

XXXXX
XXXX
XXX
XX
X
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.