Nikita Petrov
Nikita Petrov

Reputation: 51

Print is Returning Extra Parenthesis No Matter What I Do

So I am trying to code a function for practice that will make a grid.

The current code simply makes a 2x2 fancy looking grid:

First = [0, 4, 8, 12, 16]
Second = [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15]

x = 0

while True:
    if x in First:
        print '+', '-' * 4, '+', '-' * 4, '+', '-' * 4, '+', '-' * 4, '+'
        x += 1

    elif x in Second:
        print '|', ' ' * 4, '|', ' ' * 4, '|', ' ' * 4, '|', ' ' * 4, '|'
        x += 1

    else:
        break

That looks like:

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

I am trying to expand this code to make make a function that takes arguments of length and width and makes a grid of that size.

def grid_maker(length, height):

The issue I am having is that I can not for the life of me get it to correctly repeat the pattern multiple times.

If I do something like:

bob = '+', '-' * 4
print bob + bob

or

print bob * length

It has parenthesis at the start and end, as if it's making a list with each entry being bob.

Is there a way that I can repeat this pattern an x amount of times and have it print exactly as is without extra commas or parenthesis being added in?

EDIT: Using Python2.7

Upvotes: 0

Views: 54

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 113950

bob = '+', '-' * 4

is a tuple it is equivelent to

bob = ('+', '----')

I think you want

bob = '+' + '-'*4

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798576

This line:

bob = '+', '-' * 4

creates a tuple.

bob = '+' + '-' * 4

Upvotes: 3

Related Questions