jeudi 28 septembre 2023

To print a pattern in python using nested for loops

Ok I am a begginer in python The pattern to be printed w/ nested loop:

1@
1#3#
1@3@5
1#3#5#7#

(For loop preffered) thx

The code I tried:

n = int(input("Enter no:"))
for i in range(1, n + 1):
    for j in range(1, 2 * i):
        if j % 2 == 0:
            print("#", end="")
        else:
            print(str(j), end="@")
    print() 

OUTPUT:

Enter no:7
1@
1@#3@
1@#3@#5@
1@#3@#5@#7@
1@#3@#5@#7@#9@
1@#3@#5@#7@#9@#11@
1@#3@#5@#7@#9@#11@#13@

"@" seems to come every line and "#" is not printed every 2nd line

Aucun commentaire:

Enregistrer un commentaire