← CS50 Problem Sets

Problem 4

Square

Print a hollow square of hash marks, validating a size between 2 and 8.

Background

In Mario (less comfortable), you built a half-pyramid out of hash (#) characters. In this problem, you’ll build a square instead, but a hollow one: hashes around the edge, spaces in the middle.


This shows what your program’s output should look like for any size and character; it doesn’t validate input the way your program needs to.

Learning Objectives

  • Repetition with loops
  • Input validation
  • Boolean expressions and conditionals
  • print() with and without a trailing newline
  • A function beyond main()

Specification

Implement a program, square.py, that builds a hollow square of hash characters.

  • Prompt the user to enter a square size.
  • Validate that the size is an integer between 2 and 8, inclusive. If the user enters something that isn’t an integer, or an integer outside that range, don’t print an error message: just prompt again.
  • Once you have a valid size, print a hollow square of that size using hash (#) characters: hashes along the top row, the bottom row, the first column, and the last column, with spaces everywhere else in between.

For example, a size of 3 should print:

###
# #
###

And a size of 4 should print:

####
#  #
#  #
####

To Get Full Credit

It’s more important that you submit a working solution than that you do everything below. Submit early, then keep improving and resubmit as many times as you like.

  • Structure your program with at least one function besides main(). You might write one to build the square, or one to prompt for and validate the size; that part’s up to you.
  • At the end of the program, add a comment describing any challenges you ran into or what you’d improve if you did this again. A sentence or two is fine.

Usage

Your program should behave like the demo below.

 
$ python square.py
Size: 0
Size: -1
Size: 9
Size: 2
##
##

$ python square.py
Size: 4
####
#  #
#  #
####

Bonus

For extra credit: if you run your program with a single command-line argument that’s exactly one character, use that character instead of # when printing the square.

$ python square.py x
Size: 3
xxx
x x
xxx

Your program should still work fine with zero arguments; check50 needs that to still pass.

Getting Started

Log into cs50.dev, click on your terminal window, and run:

cd
mkdir square
cd square
code square.py

That creates a new folder called square, moves into it, and opens a new, empty file called square.py for you to edit.

Style and Submission

Run these one at a time, from inside your square folder.

Check your style:

style50 square.py

Check your correctness:

check50 porttack/cs50/problems/py/square

Submit your work:

submit50 porttack/cs50/problems/py/square

Glossary

  • algorithm — A finite sequence of steps that solves a problem or completes a task. Can be written in English, pseudocode, or code.
  • loop — A statement that runs one or more statements, often repeatedly. (AP calls this iteration.)
  • conditional statement — A statement that controls the flow of execution depending on some condition. Informally, this is usually an if statement, which might include an elif and an else. (AP calls this selection.)
  • boolean expression — An expression whose value is either True or False.

CC BY-NC-SA 4.0.