Home > OS >  Hollow triangle using while loops python
Hollow triangle using while loops python

Time:03-31

Trying to make a program that asks the user for a height and a character, and then outputs a hollow triangle to that height using that character. Was trying to firstly make a solid triangle, then solve it from there, but so far have only managed to make a half triangle. Also, using only for loops and no '*' operator

H = int(input("Enter height of triangle: "))
C = str(input("Character: "))
if C == "":
    C = "*"

rows = 1
count = 0
while rows <= H:
    spaces = 0
    while spaces <= (H - rows):
        print(" ", end="")
        spaces  = 1
    count = 0
    while count < rows:
        print(C, end="")
        count  = 1
    print()
    rows  = 1

this results in this:

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

my goal is this:

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

any help would be appreciated!

CodePudding user response:


Slightly changed your script:
H = int(input("Enter height of triangle: "))
C = str(input("Character: "))
if C == "":
    C = "*"

rows = 1
count = 0
while rows <= H:
    spaces = 0
    while spaces <= (H - rows):
        print(" ", end="")
        spaces  = 1
    count = 0

    while count < 2*rows-1:
        count  = 1
        if count == 1 or count == 2*rows-1 or rows == H:
            print(C, end="")
        else:
            print(" ", end="")
    print()
    rows  = 1

CodePudding user response:

  H = int(input("Enter height of triangle: "))
  C = str(input("Character: "))
  for i in range(H):
        for j in range(H - i):
              print(' ', end='')
        for j in range(2 * i   1):
              if j == 0 or j == 2 * i or i == H - 1:
                    print(C, end='')
              else:
                    print(' ', end='')
        print()

CodePudding user response:

There's already an answer, but considering that I had fun doing this little program, and that our solution are not the same :

H = int(input("Enter height of triangle: "))
C = str(input("Character: "))
if C == "":
    C = "*"

rows = 0
while rows <= H:
    cols = 0
    if rows == H:
        while cols <= H*2:
            print(C, end="")
            cols  = 1
    else:
        while cols <= H*2:
            if rows   cols == H or cols - rows == H:
                print(C, end="")
            else:
                print(" ", end="")
            cols  = 1
        print()
    rows  = 1

Note that I did this with for loops, and just swapped to while loops to paste it here.

CodePudding user response:

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

  • Related