Boxes of O's

G

geniusrko

Hi
Can anyone help with this problem

Create a big box out of n rows of little o's for any desired size n. Use an input statement to allow the user to enter the value for n and then print the properly sized box.

E.g. n = 3
oooooo
o o
oooooo

E.g. n = 8
oooooooooooooooo
o o
o o
o o
o o
o o
o o
oooooooooooooooo


using only loops
Thanks
 
G

geniusrko

Well, This is what i got

n = int(input("enter number of o: "))

for i in range(n):
print("O", end = '')
for j in range(n* 2):
print("O", end = '')

print()
 
M

MRAB

Well, This is what i got

n = int(input("enter number of o: "))

for i in range(n):
print("O", end = '')
for j in range(n* 2):
print("O", end = '')

print()
From the examples:

The first row has n*2 of 'o'
There are n-2 middle rows, each with 'o', then n*2-2 spaces, then 'o'
The last row has n*2 of 'o'

Incidentally, in your code you're writing 'O', not 'o'.
 
C

Chris Angelico

Well, This is what i got

n = int(input("enter number of o: "))

for i in range(n):
print("O", end = '')
for j in range(n* 2):
print("O", end = '')

print()

Okay! Good. Now, presumably this isn't working yet, or you wouldn't be
asking. So we just need the other crucial part of the puzzle: What is
it doing wrong? That might be an exception (copy and paste it all,
including the full traceback), or it might be producing the wrong
output, or maybe it produces the right output on everything except
some specific input.

I'll level with you here. We do not want to do your homework for you,
and we do not want anyone else to do your homework. When you're asked
to write code like this, it's not because the teacher/tutor wants the
code written - it's because you need to learn what you're doing.
Having someone else hand you perfectly working code defeats that
purpose, and it will tend to produce a sort of faux comprehension that
results in you getting a certificate and a job, while being still
quite incompetent as a programmer. Now, you may protest that it's not
as bad as that (and you'd probably be right - you have produced some
partially-working code there), but that's the extreme, and going even
part-way down that path is a Bad Thing. We, as programmers, would hate
to be coworkers to someone who glided through some course by getting
someone else to do the work; we do not want to have to try to work
with that sort of person.

(Digression: I have, and quite a few of us here probably have, worked
with exactly such a person. In my case, eventually even the boss/owner
figured out that he was a rubbish programmer; when we eventually dug
into his code thoroughly, we threw out the whole lot and literally
started over with a blank file. But before that, we found gems like
this PHP code - by the way, it *is* possible to write good PHP code,
even though the language is itself pretty appalling, but this... isn't
proof of that.

$step = 1;
do
{
switch ($step) {
case 1:
.... code code code ....
break;
case 2:
.... code code code ....
break;
default:
... raise an error ....
}
} while (++$step<2);

This code was supposed to verify certain requirements on an incoming
HTTP request. What you may not notice - and I didn't notice when I saw
this and decided to simplify the code - is that this is NOT the
for-switch paradigm, it's something even worse: a for-switch with an
off-by-one, so the only code actually executed is the "case 1" block.
And since we had properly working code in our client, none of the
errors that the "case 2" block was supposed to catch ever actually got
triggered, so we didn't see a problem... until I cleaned up the code,
and discovered that "case 2" had bugs in it. This is what happens when
you have incompetent people writing code. This is why we are strongly
disinclined to give you the answers. End digression.)

However, while we won't just give you the answers, we *will* gladly
help you to understand what's going on. So if you post code and output
(maybe an error) and ask a specific question, preferably one that
shows some level of comprehension, we will cheerfully put in quite a
bit of time to help you understand the error message, or learn how to
decipher Python tracebacks, or whatever. Helping you to understand
things makes you a better programmer, and by extension improves the
world's total programming skill; giving you the answers makes you no
better and possibly worse (by encouraging laziness), and so we don't
want to do it.

So, there you have it. Don't be a bad programmer. :) You'll find more
handy tips here - it's an invaluable resource:

http://www.catb.org/~esr/faqs/smart-questions.html

ChrisA
 
D

Dave Angel

Well, This is what i got

n = int(input("enter number of o: "))

for i in range(n):
print("O", end = '')
for j in range(n* 2):
print("O", end = '')

print()

Are you permitted to write and call functions? If so, then write
functions to solve pieces of the problem, and call those
functions from your main one. That's called factoring, and is
an important tool.

In this case, one function could print a row of 'o' of length
siz, while the second one print a 'hollow' row. Then your main
calls first, then loops calling hollow, then calls first
again.
 
G

geniusrko

I agree with you and really appreciate your experience. But what I was looking for is clues. Thank you anyway
 
G

Glazner

I agree with you and really appreciate your experience. But what I was looking for is clues. Thank you anyway

#not tested
for i in range(n):
for j in range(n*2):
if i in (0, n - 1) or j in (0, n*2 - 1):
print('o', end='')
else:
print(' ', end='')
print()
 
M

Michael Torrie

I agree with you and really appreciate your experience. But what I
was looking for is clues. Thank you anyway

Not sure what you mean. Chris certainly offered you clues to solving
this problem. Certainly he pointed you in the right direction to learn
how to approach a problem and debug code. Seems like what you really
wanted was the answer to be given you for this homework problem. And
usually someone is willing to step up and do the work for you, so you
lucked out.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,569
Members
47,206
Latest member
MalorieSte

Latest Threads

Top