-1

I'm am trying to a basic GUI which prints a space or a star depending on the number. However, my code won't print it in the format I want it to. My code is:

picture = [
  [0,0,0,1,0,0,0],
  [0,0,1,1,1,0,0],
  [0,1,1,1,1,1,0],
  [1,1,1,1,1,1,1],
  [0,0,0,1,0,0,0],
  [0,0,0,1,0,0,0]
]

for image in picture:
  for pixel in image:
    if pixel == 0:
      print(" ")
    elif pixel == 1:
      print("*")

My expected output is:

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

My actual output is:

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

This is proving really hard and help would be appreciated.

snowhow
  • 39
  • 7

1 Answers1

2

You can specify the line ends:

picture = [
  [0,0,0,1,0,0,0],
  [0,0,1,1,1,0,0],
  [0,1,1,1,1,1,0],
  [1,1,1,1,1,1,1],
  [0,0,0,1,0,0,0],
  [0,0,0,1,0,0,0]
]

for image in picture:
    for pixel in image:
        if pixel == 0:
           print(" ",end='')
        elif pixel == 1:
            print("*",end='')
    print('')

which prints

   *   
  ***  
 ***** 
*******
   *   
   *   
user_na
  • 2,154
  • 1
  • 16
  • 36