Python Forum
Putting an array for each string that is printed to a loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Putting an array for each string that is printed to a loop (/thread-15886.html)



Putting an array for each string that is printed to a loop - ClaudioSimonetti - Feb-05-2019

I would like to be able to push each | into an array

Here is my function:

def pyramide(lines):
    k = 1 * lines - 1 
    for i in range(0, lines): 
        for j in range(0, k): 
            print(end=" ") 
            k = k - 1
         for j in range(0, i+1): 
            print("|", end=" ") 
            print("\r")

lines = 5
pyramide(lines)
What I tried:

for j in range(0, i+1): 
    each = print("|", end=" ") 
    array.push(each)
    print("\r")
But it doesn't seem to add it into an array, my question is how I can push each | into an array so I can delete it later

expected input:

pyramide(5)
expected output:

    |
   | |
  | | |
 | | | |
Then I should be able to remove a | from each line by

 stickDelete(3, 2) # first paramater is the line, second is how much | would like to delete 
    |
   | |

 | | | |
Thanks for reading.


RE: Putting an array for each string that is printed to a loop - perfringo - Feb-05-2019

Do you want 'push each '|' into array' or want expected output?

You can approach this from different angle and without any complicated indices handling:

def pyramide(lines):
    for line in range(lines+1):
        print(f'{" |" * line:^{lines * 2}s}')

pyramide(5)
          
     |    
    | |   
   | | |  
  | | | | 
 | | | | |
You should keep in mind that all functions what don't return or yield anything from body will return None.

If you want to be able skip some symbols in rows then you can simply:

def pyramide(lines, row, qty):
    for line in range(lines+1):
        if line == row:
            print(f'{" |" * (line - qty):^{lines * 2}s}')
        else:
            print(f'{" |" * line:^{lines * 2}s}')
                  
pyramide(7, 5, 2)

       |      
      | |     
     | | |    
    | | | |   
     | | |    
  | | | | | | 
 | | | | | | |
Both functions print empty line at first line, to get rid of that use range(1, lines+1)