Issue
I want to show a board that is for something like sudoku: the result have some bracket which I don't want them and I don't want the last line.
board = [
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
]
for sq in board:
for row in sq:
print('|', end='')
for cell in row:
print(cell, end='|')
print()
print('+', '------------------+'*3, sep='')
if someone help me I'd be pleasure
Solution
The reason why you have brackets is because you are printing the list.
One way to unpack or print out all the values of that list as single values is to use a for loop:
>>> items = ["apple", "ball", "car"]
>>> for item in items:
>>> # Printing the values on the same line
>>> print(item, end="")
>>> apple ball car
Another thing you could do is to use a cool feature in Python to unpack the list in one line and we do this by using an astrisk (*):
>>> items = ["apple", "ball", "car"]
>>> print(*items)
>>> apple ball car
Here is the whole code including the fix for NOT having the last line:
# There are 3 squares and we dont want the line after the 3rd block.
# So we are using 'count' in order to keep track in which square
# we are in.
count = 1
for sq in board:
for row in sq:
print('|', end='')
for cell in row:
# By using a * we can unpack the list without having
# to use a for loop
print(*cell, end='|')
print()
# If we are in the 3rd square dont include a line
if count != 3:
print('+', '--------------+'*3, sep='')
count+=1
Answered By - Siddharth Dushantha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.