Getting error in , printing pattern in python

Viewed 95

I am try to print a square box using python, but every time i try, it print a straight line. So i didn't understand what's going on. Code is below.

for i in range(6):
  for j in range(6):
    print("*",end="")
    print()
4 Answers

What your code was doing:

After printing a star, the print() function prints a newline.

Try This:

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

May I suggest printing using like a function instead?

def square_stars(n):
        [print("*" * n) for i in range(n)]

Test:

square_stars(5)

Output:

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

Close - rather than printing a new line in the inner loop, you should only print it after every row is printed.

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

This will produce a 6 by 6 square of *s:

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

You should print a newline in every iteration of the outer for loop.You can try it:

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

Output will be:

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

You should print a newline in every iteration of the outer for loop. Currently, your code outputs a newline after every asterisk. Try changing the scope of the 2nd print statement.

Related