Python Forum
Putting an array for each string that is printed to a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Putting an array for each string that is printed to a loop
#1
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.
Reply
#2
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 480 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Loop over an an array of array Chendipeter 1 570 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 879 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  Loop through a list of string oldtrafford 4 1,463 Mar-24-2022, 05:30 PM
Last Post: deanhystad
  Loop through a list of string oldtrafford 3 1,688 Feb-15-2022, 04:42 PM
Last Post: snippsat
  loop for dynamic cut string - cleaner way? korenron 4 1,914 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 2,160 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,665 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,835 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Loop different actions for an array Tibovdv 4 2,748 Mar-25-2021, 06:46 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020