Home > Mobile >  Python How to draw a certain pyramid pattern
Python How to draw a certain pyramid pattern

Time:09-26

I'm trying to get these shapes:

if num=9

        *
      *   *
    *       *
  *           *
*               *

if num=5

    *
  *   *
*       *

if num=3

  * 
*   *

(Only odd inputs)

num = int(input())

for row in range(num):
    for col in range((num*2)-1):
        if row==0 and col==((num*2)-1)//2 or row==1 and (col==2 or col==6) or row==2 and (col==0 or col==8):
            print('*', end='')
        else:
            print(end=" ")
    print()

This is what I managed to write but the code is hardcoded to draw a pyramid of 5 stars, so giving it any other input would mess the shape

enter image description here

I got column and row from a quick sketch based on this image

I would like to automate the code so any input I feed it, it would draw this specific pyramid shape. I need help, thank you.

CodePudding user response:

Start thinking about how different properties relate to the input number, and make a variable for each one. Start with how many rows, and how many columns. Then think of an equation that can give you the star location based on the current column and row. This should be easy to determine from the diagram you posted.

num = int(input())

num_rows = (num // 2)   1
num_cols = (num * 2) - 1
middle_col = num - 1

for row_index in range(num_rows):
    left_star_col = middle_col - (2 * row_index)
    right_star_col = middle_col   (2 * row_index)

    for col_index in range(num_cols):
        if col_index == left_star_col or col_index == right_star_col:
            print("*", end="")
        else:
            print(" ", end="")
    print()  # newline

CodePudding user response:

Here's a way with a helper list. For each row, place a star i steps left of the middle and i steps right of the middle:

half = num // 2
for i in range(half   1):
    row = [' '] * num
    row[half-i] = row[half i] = '*'
    print(*row)

Output for num = 9 (Try it online!):

        *        
      *   *      
    *       *    
  *           *  
*               *

It prints a few spaces at the end of the upper lines. If that's troublesome, make the row just as long as needed:

half = num // 2
for i in range(half   1):
    row = [' '] * (half   i   1)
    row[half-i] = row[half i] = '*'
    print(*row)

If you replace the ' ' with '-', you can see more what's happening:

- - - - *
- - - * - *
- - * - - - *
- * - - - - - *
* - - - - - - - *

CodePudding user response:

This should give you the output very close what you desire. When you run you may see the first '*' little bit off to the left, but you can adjust for that.

def pyramid(n):
    ll = []
    ct = 0
    for row in range(n//2, -1, -1):
        l = [''] * n
        l[row] = '*'
        l[row   ct] = '*'
        ct  = 2
        ll.append(l)
    print('\n'.join(' '.join(row) for row in ll))

With minor adjustment, below should give you exactly what you need.

def pyramid(n):
    ll = []
    ct = 0
    for row in range(n//2, -1, -1):
        l = [''] * n
        l[row] = '*'
        l[row   ct] = '*'
        ct  = 2
        ll.append(l)
        l.insert(n//2, '')
    print('\n'.join(' '.join(row) for row in ll))

CodePudding user response:

One option is to define the pyramid shape as a graph: of the form y = abs(x - offset) (interactive demo here)

This can be done in python like so:

num = int(input())

for row in range(num // 2   1):
    for col in range(num * 2 - 1):
        if 2 * row == abs(num - 1 - col):
            print('*', end='')
        else:
            print(end=" ")
    print()

CodePudding user response:

Your row counter is too high, since num is the number of columns.

It's simpler if you treat row 0 specially, since it only has one * rather than 2.

Rather than looping over columns, calculate the number of spaces you need and print that many spaces using " " * count

for row in range(num//2   1):
    if row == 0:
        print(("  " * (num//2))   "*")
    else:
        print(("  " * (num//2 - row))   "*"   (" " * (row*4 - 1))   "*")
  • Related