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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  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
    #3
  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
    #4
  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
    #5
  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
    #6
  7. Ian Kelly Guest

    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
    #7
  8. Peter Otten Guest

    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
    #8
  9. Guest

    Thanks, got it now :)
     
    , Dec 12, 2012
    #9
  10. Guest

    Thanks, got it now :)
     
    , Dec 12, 2012
    #10
  11. 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
    #11
  12. 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
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luca
    Replies:
    1
    Views:
    1,053
    salman sheikh
    Apr 29, 2004
  2. Replies:
    0
    Views:
    1,255
  3. Christian

    Fix point square root

    Christian, Apr 25, 2005, in forum: VHDL
    Replies:
    5
    Views:
    6,688
    jeppe
    Mar 18, 2010
  4. Wes Groleau

    Reinventing a square wheel

    Wes Groleau, Aug 21, 2003, in forum: Perl
    Replies:
    0
    Views:
    981
    Wes Groleau
    Aug 21, 2003
  5. Protoman
    Replies:
    2
    Views:
    363
    Karl Heinz Buchegger
    Nov 10, 2005
Loading...

Share This Page