Hollow square program

Chris Angelico rosuav at gmail.com
Wed Dec 12 17:59:58 CET 2012

On Thu, Dec 13, 2012 at 3:48 AM,  <siimnurges at gmail.com> wrote:
> Hey, I need to write a program that prints out a square, which is empty on the inside. So far, I've made a program that outputs a square, which is full on the inside.
> P.S. Between every asterisk there needs to be a space(" ")
> Here's my code:
> print("Rows: ")
> rows = int(input())
> for i in range(rows):
>     for j in range(rows):
>         print("* ", end="")
>     print("")

You're going to need some kind of special handling of the beginning
and end of each row, except for the first and last rows. Since you
have i and j counting up from 0 to (rows-1), you can do this by simply
checking those two variables and then decide whether to print out a
star or a space (or, since there's spaces after the stars, whether to
print out a star and a space or two spaces).

Since you're being taught loops, I'm confident your course will have
already taught you how to make a decision in your code. It's another
Python statement.

Next time, by the way, you may want to explicitly state that this is
part of a course you're studying. Otherwise, you may come across as
sneakily trying to cheat your way through the course, which I'm sure
you would never do, would you? :) We're happy to provide hints to help
you understand Python, but not to simply give you the code (and if
anybody does, I hope you have the integrity, and desire to learn, to
ignore that post and work it out for yourself).

Have fun!


More information about the Python-list mailing list