Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Hollow square program (http://www.velocityreviews.com/forums/t955417-hollow-square-program.html)

siimnurges@gmail.com 12-12-2012 04:48 PM

Hollow square program
 
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("")

Mitya Sirenef 12-12-2012 05:18 PM

Re: Hollow square program
 
On 12/12/2012 11:48 AM, siimnurges@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("")



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/


siimnurges@gmail.com 12-12-2012 05:22 PM

Re: Hollow square program
 
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)

siimnurges@gmail.com 12-12-2012 05:22 PM

Re: Hollow square program
 
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)

siimnurges@gmail.com 12-12-2012 05:25 PM

Re: Hollow square program
 
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)

siimnurges@gmail.com 12-12-2012 05:25 PM

Re: Hollow square program
 
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)

Ian Kelly 12-12-2012 05:35 PM

Re: Hollow square program
 
On Wed, Dec 12, 2012 at 10:22 AM, <siimnurges@gmail.com> 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.

Peter Otten 12-12-2012 05:44 PM

Re: Hollow square program
 
siimnurges@gmail.com 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.


siimnurges@gmail.com 12-12-2012 05:52 PM

Re: Hollow square program
 
Thanks, got it now :)


siimnurges@gmail.com 12-12-2012 05:52 PM

Re: Hollow square program
 
Thanks, got it now :)



All times are GMT. The time now is 08:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.