Reputation: 51
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
Reputation: 113950
bob = '+', '-' * 4
is a tuple it is equivelent to
bob = ('+', '----')
I think you want
bob = '+' + '-'*4
Upvotes: 0
Reputation: 798576
This line:
bob = '+', '-' * 4
creates a tuple.
bob = '+' + '-' * 4
Upvotes: 3