Search code examples
pythonnested-loops

How do I get this pattern in Python using nested loops?


1
2 3
4 5 6
7 8 9 10

for r in range (1,5):
    for c in range (0,r):
        print(r+c, end = " ")
    print()

but, this returns output as:
1
2 3
3 4 5
4 5 6 7

Please let me know how should I get the sequence to not repeat everytime by the last digit of preceeding line....


Solution

  • current = 1
    counter = 0
    for x in range(0, 5):
        l = ""
        for y in range(current, current + x):
            counter += 1
            l = l + str(counter) + " "
        print(l)
    

    This makes

    1 
    2 3 
    4 5 6 
    7 8 9 10