271. Python Program to Print a Number Triangle
EasyPhase 1 · Warm-up
You know for loops, range() and str(). That is all you need here.
Overview
Number triangles are classic pattern programs. Each row grows from the row above it, so you learn to build a row step by step.
Problem Statement
Mei makes a counting chart for a little cousin. Read a whole number n and print n rows. Row 1 is 1, row 2 is 12, row 3 is 123, and so on. Each row counts from 1 up to the row number, with no spaces between the digits.
Steps
- Read
nand turn it into a whole number. - Start with an empty row.
- Loop over the numbers from 1 to
n. - In each turn, add the number to the end of the row, then print the row.
Examples
Example 1. Input 4 gives:
1
12
123
1234
Why: there are 4 rows, and row 4 counts from 1 to 4.
Example 2. Input 2 gives:
1
12
Why: there are 2 rows. Row 2 is row 1 with a 2 added at the end.
Input
One whole number n, from 1 to 9. From 10 on, a row would hold two-digit numbers, so n stops at 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 digits from 1 to i, 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.