livepython.net

277. Python Program to Print Letter Rows

EasyPhase 1 · Warm-up

Three tools you know are enough: a for loop over a string, * to repeat text, and break.

Overview

This pattern joins two ideas: going through the alphabet, and repeating text. Each row uses a new letter.

Problem Statement

Ravi makes a handwriting sheet for children. Each letter is written as many times as its place in the alphabet. Read a whole number n and print n rows. Row 1 is A, row 2 is BB, row 3 is CCC, and so on, with no spaces between the letters.

You can type the 26 capital letters into one string, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", and loop over it.

Steps

  1. Read n and turn it into a whole number.
  2. Make a string with the 26 capital letters, and a row count that starts at 0.
  3. Loop over the letters. In each turn, add 1 to the count and print the letter that many times.
  4. Stop the loop when the count reaches n.

Examples

Example 1. Input 4 gives:

A
BB
CCC
DDDD

Why: D is the 4th letter, so row 4 shows it 4 times.

Example 2. Input 2 gives:

A
BB

Why: there are 2 rows. B is the 2nd letter, so it is written twice.

Input

One whole number n, from 1 to 26 (the English alphabet has 26 letters). 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 i-th capital letter, 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

1A
2BB
3CCC
4DDDD

YOUR OUTPUT

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