#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Another newbie here


    I'm just learning python and I got stuck on what seems to be a relatively easy problem. I need to write a program, which outputs a square. The square's size is taken from the user input. The square needs to be empty from the inside.
    My code so far:
    Code:
    print("Rows: ")
    rows = int(input())
    for i in range(rows):
        for j in range(rows):
            print("* ", end="")
        print("")
    My code works fine, except the square is full on the inside.
    P.S. I hope you understood what I meant, english is not my native language, nor am I studying programming in english.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    156
    Rep Power
    2
    Originally Posted by supguys
    I'm just learning python and I got stuck on what seems to be a relatively easy problem. I need to write a program, which outputs a square. The square's size is taken from the user input. The square needs to be empty from the inside.
    My code so far:
    Code:
    print("Rows: ")
    rows = int(input())
    for i in range(rows):
        for j in range(rows):
            print("* ", end="")
        print("")
    My code works fine, except the square is full on the inside.
    P.S. I hope you understood what I meant, english is not my native language, nor am I studying programming in english.
    Naturally, it's printing a filled-in square because you're telling it to. Because it's a square, both i and j range over the same number but i is rows and j is columns. For the first row, and the last row, you do, indeed want every column to have "*". However, for the intervening rows, you only want "*" in the first and last column. So I would first print the first row:
    Code:
    for j in range(rows):
         print("* ", end="")
    Then for the range EXCEPT row-0 and row(Last), just print 1 "*" then columns 1 to rows-2 print " " then print 1 more "*":
    Code:
    for i in range(1,rows-1):
        print ("*",end="")
        for j in range(1,rows-1)
            print (" ",end="")
        print ("*",end="")
    Then print another row of "*"s:
    Code:
    for j in range(rows):
         print("* ", end="")
    Still better in my opinion is build 2 strings:
    Code:
    s1="*"*rows
    s2="*"+(rows-2)*" "+"*"
    Then print them as you want.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by rrashkin
    Code:
    s1="*"*rows
    s2="*"+(rows-2)*" "+"*"
    Then print them as you want.
    Thanks, that works fine, but it has one problem. I forgot to mention that there needs to be a space between each asterisk. So instead of the square's top line being like this **** it needs to be like this * * * *. And because of these spaces, the columns won't align with the rows perfectly. thanks.
    My code:
    Code:
    s1="*"*rows
    s2="*"+(rows-2)*" "+"*"
    print(s1)
    for s in range(rows-2):
        print(s2)
    print(s1)
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    156
    Rep Power
    2
    consider: "* "*n, where n is any number.

IMN logo majestic logo threadwatch logo seochat tools logo