livepython.net

274. Python Program to Print a Right-Aligned Star Triangle

EasyPhase 1 · Warm-up

You need a for loop and * to repeat text. Spaces can be repeated just like stars.

Overview

Here the triangle leans on the right side. You learn to print spaces in front of the stars, to push them to the right.

Problem Statement

Tom draws stairs for a game level. The stairs climb up to the right. Read a whole number n and print n rows:

  • Row 1 has n - 1 spaces and then 1 star.
  • Row 2 has n - 2 spaces and then 2 stars, and so on.
  • The last row has no spaces and n stars.

The stars touch: there are no spaces between them.

Steps

  1. Read n and turn it into a whole number.
  2. Loop over the row numbers from 1 to n.
  3. Make the spaces: the size minus the row number.
  4. Make the stars: as many as the row number. Print the spaces and the stars together on one line.

Examples

Example 1. Input 4 gives:

   *
  **
 ***
****

Why: row 1 has 3 spaces and 1 star. Each row has one space less and one star more, so the right edge stays straight.

Example 2. Input 2 gives:

 *
**

Why: row 1 has 1 space and 1 star. Row 2 has no spaces and 2 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. Line i has n - i spaces and then i stars. The spaces at the start of a line matter: the tests check them. The tests try 5 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.