Search code examples
pythondimensions

dimensions of a +--+ box in Python 3.4


This is my problem

Create a program that accepts a number inputted by the user. This number defines the dimensions of a square and can be any positive integer. An input of 1 will output:

    +--+
    +--+

while an input of 2 will output

    +--+--+
    +--+--+
    +--+--+

and an input of 3 will output

    +--+--+--+
    +--+--+--+
    +--+--+--+
    +--+--+--+

etc…Show outputs for user inputs of 1, 2, 3, and 4. 

Not entirely sure where to start on this one and would like some advice, however i'm not looking for a complete answer (after all, this is homework) but something to point me in the right direction would be very much appreciated.


Solution

  • Consider this:

    "--".join("++")
    

    Gives you one line of one box:

    +--+
    

    To repeat for multiple lines, you can do:

    "--".join("+" * (some_count+1))
    

    For this, you'd get the output:

    +--+        # 1
    +--+--+     # 2
    +--+--+--+  # 3
    ...
    

    Now we just need to repeat that for however many vertical lines. You could consider doing "\n".join to repeat, or you could use a for loop and print over multiple lines. That much is your job!