2

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.

Kai Wu Toh
  • 236
  • 2
  • 17
Ronincat
  • 31
  • 3
  • `for _ in range(n + 1): print("--".join("+" * (n + 1)))` or something. – Veedrac Jan 02 '16 at 07:23
  • 2
    One step at a time. First create a program that accepts a number input by a user. Done that? Now make sure that is a positive number, remember that `raw_input()` (assuming Python 2) returns a string. Get all that working first. Now investigate using either a loop, or maybe even the `*` operator on a string, there are several ways to do this. – cdarke Jan 02 '16 at 07:24
  • Much appreciated, thank you – Ronincat Jan 02 '16 at 07:24

2 Answers2

2

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!

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Ended up with

"""Problem set 2 question 2 box problem Joe White 2016"""
num=int(input("enter a number ")) #Assigns user's number to variable num
for blank in range (num+1): print ("--".join("+" * (num+1))) #print ("--".join("+" * (num+1))): prints +--+ as many times as the user entered. However if more than 1 it prints in the form +--+--+ instad of +--++--+. for blank in range (num+1) adds another column of +--+ underneath to create a box 

Works perfectly for all outputs. Thank you to everyone who posted a comment/answer!

Ronincat
  • 31
  • 3
  • you should use `_` instead of `blank` if you're just throwing away the loop variable. There's nothing special about `_` in Python, but it is idiomatically used to tell future coders that you don't use the variable. `blank` could have meaning, `_` never will. – Adam Smith Jan 02 '16 at 08:46
  • @AdamSmith. _ can have meaning if you are doing internationalization. An explicit name that indicates that the value is being discarded can make the code more legible and less error prone. – Mad Physicist Jan 04 '16 at 03:34
  • @MadPhysicist we have one in Python. It's `_`. `for _ in some_iterator` is found everywhere in idiomatic Python code. – Adam Smith Jan 04 '16 at 03:51
  • Though I'm having trouble seeing when/why you'd use `_` as an identifier during internationalization. Can you explain? – Adam Smith Jan 04 '16 at 03:54
  • 1
    @AdamSmith, see http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python – Mad Physicist Jan 04 '16 at 11:36
  • @MadPhysicist TIL! What an interesting convention. Though certainly not worth following dogmatically, if you have `_` in your namespace for whatever reason ***and*** you have to use that name inside your `for` loop, you should instead use `__` (double underscore) for your throwaway. – Adam Smith Jan 04 '16 at 17:00
  • 1
    Thanks. I found that post because the first time I came across `_` it confused the *** out of me. – Mad Physicist Jan 04 '16 at 17:03