livepython.net

285. Python Program to Print an Upside-Down Star Pyramid

MediumPhase 2 · Practice

A few steps, all with tools you know: a for loop, a range() that counts down, and * to repeat text.

Overview

Here the pyramid stands on its tip. The trick is to count the rows backwards, so the widest row comes first.

Problem Statement

Omar draws a funnel for a kitchen app. A funnel is wide at the top and narrow at the bottom. Read a whole number n and print n rows:

  • Row 1 has no spaces and 2 * n - 1 stars.
  • Each new row has 1 space more and 2 stars less.
  • The last row has n - 1 spaces and 1 star.

The stars touch: there are no spaces between them.

Examples

Example 1. Input 4 gives:

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

Why: row 1 has 7 stars, because 2 * 4 - 1 is 7. Each row has 1 space more and 2 stars less, so the last row has 3 spaces and 1 star.

Example 2. Input 2 gives:

***
 *

Why: row 1 has 3 stars and no spaces. Row 2 has 1 space and 1 star, so the star sits under the middle star.

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. Line i has i - 1 spaces and then 2 * (n - i) + 1 stars. The spaces at the start of a line 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.

Finish this chapter firstChapter 10 · For Loops
solution.py
# write your code here

EXPECTED OUTPUT

1*******
2 *****
3 ***
4 *

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.