livepython.net

272. Python Program to Print a Row-Number Triangle

EasyPhase 1 · Warm-up

A for loop, str() and * to repeat text are enough here. You have used all three.

Overview

In this pattern, each row repeats its own row number. It trains you to see the link between the row number and what goes in the row.

Problem Statement

Kofi makes a poster for a nursery class. Each number is shown as many times as its value. Read a whole number n and print n rows. Row 1 is 1, row 2 is 22, row 3 is 333, and so on, with no spaces between the digits.

Steps

  1. Read n and turn it into a whole number.
  2. Loop over the numbers from 1 to n.
  3. Turn the number into text.
  4. Print that text, repeated as many times as the number.

Examples

Example 1. Input 4 gives:

1
22
333
4444

Why: row 4 shows the digit 4, four times.

Example 2. Input 2 gives:

1
22

Why: there are 2 rows. Row 1 has one 1, and row 2 has two 2s.

Input

One whole number n, from 1 to 9. 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 holds the digit i, written i times, with no spaces. Every line starts at the left edge, with no space in front. 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

11
222
3333
44444

YOUR OUTPUT

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