270. Python Program to Print a Square of Stars
EasyPhase 1 · Warm-up
This is the first pattern. You need one for loop and * to repeat text, and you know both.
Overview
Pattern programs print shapes with characters. They train you to think in rows: what goes in one row, and how many rows there are.
Problem Statement
Chen makes a small game map. Each tile on the map is a square of stars. Read the size n and print a square: n rows, and n stars in each row, with one space between the stars.
Steps
- Read
nand turn it into a whole number. - Make one row: a star and a space,
ntimes. - Print that row
ntimes with a loop.
Examples
Example 1. Input 3 gives:
* * *
* * *
* * *
Why: 3 rows, and each row has 3 stars.
Example 2. Input 1 gives:
*
Why: a square of size 1 is one row with one star.
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 of n stars. Every line starts at the left edge, with no space in front. A space at the end of a line is fine: the tests ignore it. The tests try 4 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.