Hollow square program

S

siimnurges

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

Mitya Sirenef

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
 
S

siimnurges

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)
 
S

siimnurges

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)
 
S

siimnurges

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)
 
S

siimnurges

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)
 
I

Ian Kelly

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.
 
P

Peter Otten

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.
 
M

Mark Lawrence

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>
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top