Python hollow triangle while loop

The way to get a hollow triangle is to print spaces in a loop. If you observe the output you need, you'll see that except for the top and bottom lines, every line has only 2 asterisks [*]. That means you need a logic that handles spaces.

There are several ways to write the logic, such as treating each vertical halves as blocks of fixed length and just varying the position of the star or actually counting the spaces for each line. You can explore the different ways to achieve what you need at your convenience. I'll present one soln.

H = int[input["Enter height of triangle: "]]
C = str[input["Character: "]]
if len[C] != 1:
    C = "*"

rows = 1
count = 0
while rows < H:
    str = ""
    for i in range[H - rows]:
        str += " "
    str += C

    if rows > 1:
        for i in range[2 * rows - 3]:
            str += " "
        str += C 

    print[str]
    rows += 1

str = ""
for i in range[2 * H - 1]:
    str += C
print[str]

I have made a change about checking the character. You should not allow characters of more than length 1. Otherwise, the spacing will get messed up

These exercises are meant for you to understand the logic and get comfortable with manipulating code, so do try different variations

This is probably not the most optimized solution but, remember that printing is in general slow as it has to interact with a peripheral [monitor], so try to print in bulk whenever possible. This improves the speed

This python program generates hollow pyramid pattern made up of stars up to n lines.

In this python example, we first read number of row in the hollow pyramid pattern from user using built-in function input[]. And then we use using python's for loop to print hollow pyramid pattern.

Python Source Code: Pyramid Pattern


# Generating Hollow Pyramid Pattern Using Stars

row = int[input['Enter number of rows required: ']]

for i in range[row]:
    for j in range[row-i]:
        print[' ', end=''] # printing space required and staying in same line
    
    for j in range[2*i+1]:
        if j==0 or j==2*i or i==row-1:
            print['*',end='']
        else:
            print[' ', end='']
    print[] # printing new line

In this program print[] only is used to bring control to new lines.

Output: Pyramid Pattern

Enter number of rows required: 12
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *               *
   *                 *
  *                   *
 ***********************

Python Pattern Programs using While Loop

In this tutorial, we will learn how to print patterns to console using Python While Loop.

Example 1 – Python Program to Print Right Triangle using While Loop

In this example, we will write a Python program to print the following start pattern to console. We shall read the number of rows and print starts as shown below.

Pattern

For an input number of 4, following would be the pattern.

*
* *
* * *
* * * *

Try Online

Python Program

n = int[input['Enter number of rows : ']]

i = 1
while i 

Chủ Đề