0

Hi I would like create some code which will print a box that looks like this

 + -- + -- + -- + -- + -- +
 |    |    |    |    |    |
 + -- + -- + -- + -- + -- +

The code should use a loop to print a row of boxes using for i in range(5)(There should be no use of IF statement to solve this problem) by using one box only as shown below

+ -- +
|    |
+ -- +

I have attempted to use the code below but not producing the output required. Please help

for i in range(5):
    print("+--+\n|  |\n+--+", end=" ")
3
  • 2
    You need to rethink your approach. You clearly can't just print a single box five times--not only will you repeat "walls" that way, but if each box contains newlines, there's no way to "stack" them horizontally. You'll need to do the loop first, create the three lines you need to print, and then print them out once they're complete. Commented Nov 18, 2014 at 20:32
  • This looks a lot like a homework question. Why the restriction on using that specific loop construct? Commented Nov 18, 2014 at 20:38
  • What is the desired behavior for printBoxes(0)? A vertical line, or no drawing at all? Commented Nov 19, 2014 at 2:35

3 Answers 3

4

You can do this with a combination of using the * operator to make a string of characters, and join to add delimiters between those characters.

def printBoxes(boxes):
    edges = ' -- '.join('+' * (boxes+1))
    middle = '    '.join('|' * (boxes+1))
    print(edges)
    print(middle)
    print(edges)

Testing

>>> printBoxes(3)
+ -- + -- + -- +
|    |    |    |
+ -- + -- + -- +

>>> printBoxes(5)
+ -- + -- + -- + -- + -- +
|    |    |    |    |    |
+ -- + -- + -- + -- + -- +
Sign up to request clarification or add additional context in comments.

4 Comments

Nitpick-- there are no cubes here. ;-)
:P Fair enough, renamed
The homework required you to use a loop, so you'll have to re-write this using unnaturally clumsy code. Though I guess just replacing the print statements with for line in (edges, middle, edges): print(line) meets the problem as stated.
@Duncan: How does that meet the requirement of "using for i in range(5)"? I suppose you could do something really artificial, like for i in range(5): print((edges+'\n', middle+'\n', edges+'\n', '', '')[i], end=''), but that would be pretty silly…
2

I think the way your teacher wants you to solve this is by using the for loop to build up the three lines, box by box, and then print all three lines. Or, noticing that the top and bottom are the same, just use the same line for each:

edge, middle = '+', '|'
for i in range(5):
    edge += ' -- +'
    middle += '    |'
print(edge)
print(middle)
print(edge)

However, the answers using the * string repetition operator and/or the join method are much more Pythonic. If you can explain to your teacher how they work, and why they're better, and if your teacher isn't overly rigid (or stupid), that might be worth doing. Even if you don't want to try that, learning why they're better on your own may be worth doing.

Comments

0
def printBoxes(n):
    top = "+--" * n + "+"
    middle = "|  " * n + "|"

    print(top)
    print(middle)
    print(top)

printBoxes(5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.