# Hollow square program

Discussion in 'Python' started by siimnurges@gmail.com, Dec 12, 2012.

1. ### Guest

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("")

, Dec 12, 2012

2. ### Mitya SirenefGuest

On 12/12/2012 11:48 AM, 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("")

Small note: print("") is the same as print()

The general idea is: make a string that represents top/bottom of the
square, make a string for the middle part, print out the first string,
then print out middle part n-2 times, then print out first string again.

Did you know you can multiply strings? e.g.:

space = ' '
border = '*'
line = border + space*70

-m

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Mitya Sirenef, Dec 12, 2012

3. ### Guest

Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help

rows = int(input())
s1="* "*rows
s2="*"+(rows-2)*" "+"*"
print(s1)
for s in range(rows-2):
print(s2)
print(s1)

, Dec 12, 2012
4. ### Guest

Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help

rows = int(input())
s1="* "*rows
s2="*"+(rows-2)*" "+"*"
print(s1)
for s in range(rows-2):
print(s2)
print(s1)

, Dec 12, 2012
5. ### Guest

Well, I did some modifications, but the columns aren't perfectly aligned with the rows.

rows = int(input())
s1="* "*rows
s2="*"+(rows-2)*" "+"*"
print(s1)
for s in range(rows-2):
print(s2)
print(s1)

, Dec 12, 2012
6. ### Guest

Well, I did some modifications, but the columns aren't perfectly aligned with the rows.

rows = int(input())
s1="* "*rows
s2="*"+(rows-2)*" "+"*"
print(s1)
for s in range(rows-2):
print(s2)
print(s1)

, Dec 12, 2012
7. ### Ian KellyGuest

On Wed, Dec 12, 2012 at 10:22 AM, <> wrote:
> Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help
>
> rows = int(input())
> s1="* "*rows
> s2="*"+(rows-2)*" "+"*"
> print(s1)
> for s in range(rows-2):
> print(s2)
> print(s1)

The (rows-2)*" " in s2 is only enough spaces to account for the
(rows-2) inner * characters in s1. You also need additional spaces in
s2 to match up with for the (rows-1) space characters in between the *
characters in s1.

Ian Kelly, Dec 12, 2012
8. ### Peter OttenGuest

wrote:

> Well, I did some modifications and got a hollow square, but the columns
> aren't perfectly aligned with the rows (at least if input is 5. Thanks for
> the help
>
> rows = int(input())
> s1="* "*rows
> s2="*"+(rows-2)*" "+"*"
> print(s1)
> for s in range(rows-2):
> print(s2)
> print(s1)

The first and last row have an extra space between the asterisks. For the
square to look like a square you need to add these to the other rows (and
thus s2), too.

Also note that your code prints a minimum of two rows.

Peter Otten, Dec 12, 2012
9. ### Guest

Thanks, got it now

, Dec 12, 2012
10. ### Guest

Thanks, got it now

, Dec 12, 2012
11. ### Mark LawrenceGuest

On 12/12/2012 17:52, wrote:
> Thanks, got it now
>

Please quote something in context for future readers. Roughly translated

<sorry for shouting, apologies if I'm wrong>

THE ONLY GOOD GOOGLE USER IS A DEAD GOOGLE USER

</sorry for shouting, apologies if I'm wrong>

--
Cheers.

Mark Lawrence.

Mark Lawrence, Dec 12, 2012
12. ### Steven D'ApranoGuest

On Wed, 12 Dec 2012 23:47:20 +0000, Mark Lawrence wrote:

> THE ONLY GOOD GOOGLE USER IS A DEAD GOOGLE USER
>
> </sorry for shouting, apologies if I'm wrong>

I say, that's a bit much. That crosses a line from a friendly contempt
for ignorant Gmail users to something rather nasty. Preemptive apology or
not, I don't think that is acceptable here.

--
Steven

Steven D'Aprano, Dec 13, 2012