Newbie coder here. I am working on a project for school where you create a 10x10 grid in Python. This grid will ultimately be used for the game Battleship. Our teacher has asked us to initialize the grid using '.' to signify open water. The output should look like: desired output
When I run my code, I am getting '' instead of . and also have brackets around each row that I would like to remove.
See the attached picture for the code I have so far.
#global variable for grid size
grid_size = 10
#global variable for grid
myBoard = [ ['']*grid_size for i in range (grid_size) ]
# display the grid (2d array)
def main(myBoard):
def drawBoard(myBoard):
for i in range(grid_size):
for j in range(grid_size):
print(grid[i][j], end = "")
print()
def setupBoard(myBoard):
i = j = 0
while i < grid_size:
while j < grid_size:
grid[i][j] = '.'
j = 0
i += 1
main(myBoard)
print(myBoard)
I tried changing grid = [ ['']*grid_size for i in range (grid_size) ]
To grid = [ (.)*grid_size for i in range (grid_size) ]
But received an invalid syntax message. I tried changing it to ['.'] as well and that worked but showed the quote and bracket marks, so I'm not sure how to get rid of those. Any feedback would be greatly appreciated!
There are a couple of issues with your code. The major one is that you define functions below main()
. What you're trying to do is call them in main()
, but their definitions should be outside of the main()
function definition.
Additionally, you use the variable grid
in various places in your code, but grid
is not defined anywhere. The variable that you wanted to use is myBoard
instead of grid
Finally, if you use the following to intialize myBoard
:
myBoard = [['.']*grid_size for i in range(grid_size)]
The function setupBoard()
becomes useless, because it is attempting - there are some errors in it - to achieve the same thing as the line of code above.
Fixing the issues mentioned the code could look like this:
#global variable for grid size
grid_size = 10
#global variable for grid
myBoard = [['.']*grid_size for i in range(grid_size)]
# display the grid (2d array)
def main():
# Call drawBoard function with myBoard
drawBoard(myBoard)
def drawBoard(myBoard):
# Print header
print(end=" ")
for i in range(grid_size):
print(i, end=" ")
print()
# Print rows
for i in range(grid_size):
print(i, end=" ")
for j in range(grid_size):
print(myBoard[i][j], end=" ") # change grid to myBoard
print()
# Call main function
main()
This program outputs the following:
0 1 2 3 4 5 6 7 8 9
0 . . . . . . . . . .
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
4 . . . . . . . . . .
5 . . . . . . . . . .
6 . . . . . . . . . .
7 . . . . . . . . . .
8 . . . . . . . . . .
9 . . . . . . . . . .
Note 1: When a character(s) have quotes around them '
or "
, it signifies that it is a string
. If the character(s) did not have quotes, the python compiler assumes that it is a variable and looks for its definition. Since it is not defined, it will throw an error.
Note 2: When a string is printed into the console, the quotes are omitted, unless the are escaped \"
.