1

I am doing exercise 3.3 from Think Python book which asks us to write a program without using for loops to print the following grids (using only the concepts introduced so far - string replication using * operator, simple functions and passing functions as arguments)

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

def print_line(start_char, middle_char):
    print(start_char, middle_char * 4, end=" ")
    print(start_char, middle_char * 4, start_char)


def draw_grid():
    # print("+", "- " * 4, "+", "- " * 4, "+")
    print_line("+", "- ")
    # print("|", "  " * 4, "|", "  " * 4, "|")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("+", "- ")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("|", "  ")
    print_line("+", "- ")


if __name__ == "__main__":
    draw_grid()

Here are questions I have:

  1. Is there a better way of doing this in python. I am new to python and would like to use this simple program to deepen python constructs

  2. In print_line function, I wanted to repeat the printing of "+ - - - -" twice. Hence, I wanted to see whether I can replicate the combined string. But when I used brackets around like print((start_char, middle_char * 4)*2), it ended up printing brackets. Is there a way to achieve this

1
  • 1
    Your grid has an extra space before the second +. Perhaps use end="" instead. Commented Feb 26, 2020 at 4:37

1 Answer 1

3
print((start_char, middle_char * 4)*2)

This creates a tuple containing two elements.

I think what you may have meant to do was this;

print((start_char + middle_char * 4)*2)
#                 ^ This concats the strings, instead of making a tuple.

I think that should be enough to get you back onto the right path.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear it :) Feel free to mark this as answered if it helped you on your way.

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.