livepython.net

276. Python Program to Print a Letter Triangle

EasyPhase 1 · Warm-up

You know how a for loop goes through a string, and you know break. That is all you need here.

Overview

Letter patterns work like number patterns, but with letters. They show you how to go through a string one letter at a time.

Problem Statement

Yuki makes an alphabet chart for a reading class. Read a whole number n and print n rows. Row 1 is A, row 2 is AB, row 3 is ABC, and so on. Each row has one more capital letter than the row before it, 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 an empty row.
  3. Loop over the letters. In each turn, add the letter to the row and print the row.
  4. Stop the loop when the row has n letters.

Examples

Example 1. Input 4 gives:

A
AB
ABC
ABCD

Why: there are 4 rows, and the last row has the first 4 letters.

Example 2. Input 2 gives:

A
AB

Why: there are 2 rows. Row 2 is row 1 with a B added at the end.

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 first i capital letters, 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
2AB
3ABC
4ABCD

YOUR OUTPUT

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