282. Python Program to Print a Plus Sign of Stars
EasyPhase 1 · Warm-up
You know for loops, if and the // sign. That is all you need here.
Overview
A plus sign has a full middle row and one star in the middle of every other row. To draw it, you find the middle first.
Problem Statement
Zara paints a first-aid sign for a school trip box. Read an odd whole number n and print a plus sign of n rows:
- The middle row is
nstars, with no spaces in front and no spaces between them. - Every other row has one star in the middle. In front of it are
n // 2spaces.
Steps
- Read
nand turn it into a whole number. - Work out the middle:
ndivided by 2, with the part after the point dropped. - Loop over the rows.
- In the middle row, print
nstars. In every other row, print the spaces and one star.
Examples
Example 1. Input 5 gives:
*
*
*****
*
*
Why: 5 // 2 is 2, so each single star has 2 spaces in front of it. The middle row is row 3, with 5 stars.
Example 2. Input 3 gives:
*
***
*
Why: 3 // 2 is 1, so the top and bottom rows have 1 space and a star.
Input
One odd whole number n, 3 or more (3, 5, 7 and so on). 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. The spaces at the start of a line matter: the tests check them. 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.