Home > Enterprise >  Can anyone help me create a nested loop with an increment?
Can anyone help me create a nested loop with an increment?

Time:03-27

What I mean is a nested loop that will print out symbols. The number of symbols should be determined by incrementing the rows. At the same time the column of symbols should run for a set number before moving to the next number. The outcome will look something like below.

@

@

@@

@@

@@@

@@@

and so on.

Only managed to write this: rows = 5

outer loop

symbol = "@"

for i in range(rows):

# nested loop for j in range(i): print(symbol, end=' ')

print('') rows = 5

Tried this: rows = 2

outer loop

symbol = "@"

for i in range(rows):

# nested loop

for j in range(i):

print(symbol, end=' ')

print('') rows =2

I was expecting an output

@

@

@@

@@

@@@

@@@

CodePudding user response:

I think you want this :

n = int(input())

for i in range(1,n 1):
    for j in range(1,i 1):
        print("@"*i)

output:

enter image description here

Edited :

I think you want as many as n repeat @ So this is :

n = int(input())

for i in range(1,n 1):
    for j in range(1,n 1):
        print("@"*i)

output:

enter image description here

This is a Matrix and you should play with it. it could be n*n or n*m means 2 * 2 or 2*3 or ...

for example this is n 1 * n :

n = int(input())

for i in range(1,n 2):
    for j in range(1,n 1):
        print("@"*i)

enter image description here

CodePudding user response:

Thanks very much @parisa-h-r. I added a separate input for the outer loop from the code you provided and it worked.

n = int(input("This is the row: "))

m = int(input("This is the column: ")) for i in range(n 1): for j in range(m): print("@"*i)

enter image description here

  • Related