287. Python Program to Print a Hollow Triangle
MediumPhase 2 · Practice
There are a few steps here. Each one uses a tool you know: a for loop, if with elif, and * to repeat text.
Overview
A hollow shape has stars only on its edges. Here you take a star pyramid and keep only its outline, so each row needs its own rule.
Problem Statement
Diego draws the outline of a tent for a camping map. Read a whole number n and print n rows:
- Row 1 has
n - 1spaces and 1 star: the top of the tent. - Every middle row
ihasn - ispaces, a star,2 * i - 3spaces and a star. - The last row is full:
2 * n - 1stars with no spaces.
This puts every star on the edge of a star pyramid of n rows. The inside is empty.
Examples
Example 1. Input 5 gives:
*
* *
* *
* *
*********
Why: row 3 has 2 spaces, a star, 3 spaces and a star, because 2 * 3 - 3 is 3. The last row is full, with 2 * 5 - 1 = 9 stars.
Example 2. Input 3 gives:
*
* *
*****
Why: row 2 is the only middle row: 1 space, a star, 1 space and a star. The last row has 5 stars.
Input
One whole number n (1 or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
n lines. For n = 1, the triangle is a single star. The spaces at the start of a line and the spaces inside it matter: the tests check them. The tests try 6 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.