Skip to content
Advertisement

How to write diagonal spaced print pattern in python [closed]

if n=2

* *
 * *
* *
 * *

if n=3

*  *  *
 *  *  *
  *  *  *
*  *  *
 *  *  *
  *  *  *
*  *  *
 *  *  *
  *  *  *

This code i written for. But this code is half this only print row and column not space condition.

n = int(input (": "))
i = 0
for i in range (n**2):
    j = 0
    for j in range (n):
        j+=1
        print ("* " , end="")
    i+=1
    print ("")

Advertisement

Answer

You should remove i = 0, i += 1, j = 0, and j += 1. These operations happen by default in for loops.

n = int(input(": "))
for i in range(n ** 2):

    for k in range(i % n):
        print(" ", end="")

    for j in range(n):
        print("* ", end="")

    print("")

Or you can make your string first, then print it:

n = int(input(": "))
for i in range(n ** 2):
    line = ((i % n) * ' ') + (n * '* ')
    print(line)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement