1

I tried various programs to get the required pattern (Given below). The program which got closest to the required result is given below:

Input:

for i in range(1,6):
    for j in range(i,i*2):
        print(j, end=' ')
    print( )

Output:

1 
2 3 
3 4 5 
4 5 6 7 
5 6 7 8 9 

Required Output:

1
2 3
4 5 6
7 8 9 10 

Can I get some hint to get the required output?

Note- A newbie to python.

2 Answers 2

1

Store the printed value outside of the loop, then increment after its printed

v = 1
lines = 4
for i in range(lines):
    for j in range(i):
        print(v, end=' ')
        v += 1
    print( )
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, what does this print() do? I don't get how we shift to a new line using print().
Well, you had that in the original question. By default, that prints a new line character.
0

If you don't want to keep track of the count and solve this mathematically and be able to directly calculate any n-th line, the formula you are looking for is the one for, well, triangle numbers:

triangle = lambda n: n * (n + 1) // 2
for line in range(1, 5):
    t = triangle(line)
    print(' '.join(str(x+1) for x in range(t-line, t)))
# 1
# 2 3
# 4 5 6
# 7 8 9 10

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.