Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34698 > unrolled thread

Hollow square program

Started bysiimnurges@gmail.com
First post2012-12-12 08:48 -0800
Last post2012-12-12 09:25 -0800
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Hollow square program siimnurges@gmail.com - 2012-12-12 08:48 -0800
    Re: Hollow square program Chris Angelico <rosuav@gmail.com> - 2012-12-13 03:59 +1100
      Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:22 -0800
        Re: Hollow square program Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-12 10:35 -0700
        Re: Hollow square program Peter Otten <__peter__@web.de> - 2012-12-12 18:44 +0100
          Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:52 -0800
            Re: Hollow square program Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-12 23:47 +0000
              Re: Hollow square program Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-13 00:12 +0000
          Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:52 -0800
      Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:22 -0800
    Re: Hollow square program Mitya Sirenef <msirenef@lightbird.net> - 2012-12-12 12:18 -0500
      Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:25 -0800
      Re: Hollow square program siimnurges@gmail.com - 2012-12-12 09:25 -0800

#34698 — Hollow square program

Fromsiimnurges@gmail.com
Date2012-12-12 08:48 -0800
SubjectHollow square program
Message-ID<1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com>
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("")

[toc] | [next] | [standalone]


#34699

FromChris Angelico <rosuav@gmail.com>
Date2012-12-13 03:59 +1100
Message-ID<mailman.779.1355331602.29569.python-list@python.org>
In reply to#34698
On Thu, Dec 13, 2012 at 3: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("")

You're going to need some kind of special handling of the beginning
and end of each row, except for the first and last rows. Since you
have i and j counting up from 0 to (rows-1), you can do this by simply
checking those two variables and then decide whether to print out a
star or a space (or, since there's spaces after the stars, whether to
print out a star and a space or two spaces).

Since you're being taught loops, I'm confident your course will have
already taught you how to make a decision in your code. It's another
Python statement.

Next time, by the way, you may want to explicitly state that this is
part of a course you're studying. Otherwise, you may come across as
sneakily trying to cheat your way through the course, which I'm sure
you would never do, would you? :) We're happy to provide hints to help
you understand Python, but not to simply give you the code (and if
anybody does, I hope you have the integrity, and desire to learn, to
ignore that post and work it out for yourself).

Have fun!

ChrisA

[toc] | [prev] | [next] | [standalone]


#34701

Fromsiimnurges@gmail.com
Date2012-12-12 09:22 -0800
Message-ID<1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com>
In reply to#34699
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)

[toc] | [prev] | [next] | [standalone]


#34706

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-12-12 10:35 -0700
Message-ID<mailman.784.1355333752.29569.python-list@python.org>
In reply to#34701
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.

[toc] | [prev] | [next] | [standalone]


#34708

FromPeter Otten <__peter__@web.de>
Date2012-12-12 18:44 +0100
Message-ID<mailman.785.1355334206.29569.python-list@python.org>
In reply to#34701
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.

[toc] | [prev] | [next] | [standalone]


#34711

Fromsiimnurges@gmail.com
Date2012-12-12 09:52 -0800
Message-ID<f03de3bb-da97-4cff-a3b5-1488b9bf279b@googlegroups.com>
In reply to#34708
Thanks, got it now :)

[toc] | [prev] | [next] | [standalone]


#34735

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-12-12 23:47 +0000
Message-ID<mailman.809.1355356055.29569.python-list@python.org>
In reply to#34711
On 12/12/2012 17:52, siimnurges@gmail.com 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.

[toc] | [prev] | [next] | [standalone]


#34737

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-13 00:12 +0000
Message-ID<50c91d58$0$29972$c3e8da3$5496439d@news.astraweb.com>
In reply to#34735
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

[toc] | [prev] | [next] | [standalone]


#34712

Fromsiimnurges@gmail.com
Date2012-12-12 09:52 -0800
Message-ID<mailman.788.1355334750.29569.python-list@python.org>
In reply to#34708
Thanks, got it now :)

[toc] | [prev] | [next] | [standalone]


#34702

Fromsiimnurges@gmail.com
Date2012-12-12 09:22 -0800
Message-ID<mailman.781.1355332959.29569.python-list@python.org>
In reply to#34699
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)

[toc] | [prev] | [next] | [standalone]


#34700

FromMitya Sirenef <msirenef@lightbird.net>
Date2012-12-12 12:18 -0500
Message-ID<mailman.780.1355332724.29569.python-list@python.org>
In reply to#34698
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/

[toc] | [prev] | [next] | [standalone]


#34703

Fromsiimnurges@gmail.com
Date2012-12-12 09:25 -0800
Message-ID<7f16f3d5-4904-4880-af56-20b8d11c474b@googlegroups.com>
In reply to#34700
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) 

[toc] | [prev] | [next] | [standalone]


#34704

Fromsiimnurges@gmail.com
Date2012-12-12 09:25 -0800
Message-ID<mailman.782.1355333170.29569.python-list@python.org>
In reply to#34700
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) 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web