Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Hollow square program

Reply
Thread Tools

Hollow square program

 
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
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("")
 
Reply With Quote
 
 
 
 
Mitya Sirenef
Guest
Posts: n/a
 
      12-12-2012
On 12/12/2012 11:48 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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/

 
Reply With Quote
 
 
 
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
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)
 
Reply With Quote
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
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)
 
Reply With Quote
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
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)
 
Reply With Quote
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
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)
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      12-12-2012
On Wed, Dec 12, 2012 at 10:22 AM, <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-12-2012
(E-Mail Removed) 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.

 
Reply With Quote
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
Thanks, got it now

 
Reply With Quote
 
siimnurges@gmail.com
Guest
Posts: n/a
 
      12-12-2012
Thanks, got it now

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD Verdict reviews: HOLLOW MAN 2, SPLIT SECOND, EVILS OF THE NIGHT, and more! DVD Verdict DVD Video 0 05-19-2006 08:27 AM
CG animated Sleepy Hollow? Ollie T DVD Video 4 10-08-2005 01:00 AM
Will we ever see Disney's The Legend of Sleepy Hollow on DVD? Mac Cool DVD Video 4 10-12-2004 04:52 AM
SRT DIvision, Square root and reciprocal square root alghazo@siu.edu VHDL 0 05-27-2004 06:23 AM
help backing up hollow man uk version!!!!! Jibby DVD Video 3 10-20-2003 01:12 PM



Advertisments