samedi 30 octobre 2021

Q: [Python] How to print a pyramid of full pyramids?

So I'm a newbie programmer, and we were recently tasked with printing a full pyramid of asterisks with the number of rows being based on the input, then printing a pyramid of those pyramids.

Basically the output I'm expecting when the user inputs Number of Rows: 4 is:

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

This is the best that I could work on using looping.

rowInput = int(input("Number of Rows: "))
rowCount = 0
min = rowInput
max = rowInput

while (rowCount < rowInput):
  digit = 1
  while (digit < min): 
    print(end=" ")
    digit += 1
  while (digit >= min and digit <= max):
    print(end="*")
    digit += 1
    
  print()
  min -= 1
  max += 1
  rowCount += 1

Input:

Number of Rows: 4

Output:

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

I think that it could work with another loop on top, but I can't think of a viable way to increment the spaces between two triangles. Are there any other options?

Aucun commentaire:

Enregistrer un commentaire