# Battleship: Printing Pretty

#1

Hi,

I have no idea what I’m doing. This is supposed to print out five row of O’s with nothing else but it doesn’t work. Even if it did work, I’d have no idea why. Thanks for any help.

oard = []

range(5)

for each_number in range(5):
board.append(["O"] * 5)

def print_board(board_in):
for row in range(5):

print board[row] "  ".join(row)

print_board(board)

#2

That statement suggests a complete review of the basics is in order. Have you considered going back to the start of the track and doing everything over again? Practice makes perfect, as they say.

Consider data structures, for instance. We learned that Python has a number of structures, each with their own purposes and properties.

list        => []
tuple       => ()
set         => {}
dictionary  => {}

A list is ordered by index. An index is a sequence that starts at 0 and increases by 1 with each ‘step’ from left to right (top to bottom, as it were). Lists are mutable which means that we can modify them by appending, inserting, deleting, or changing the value at any data point. As such, and since this game changes the data, it is suited for our use.

board = []

That is an initialized variable that references an empty list.

A range is a type of list called an iterable. When we define a range we can specify the starting point, the ending point, and the step value, the difference between each value in the sequence.

fives = list(range(5, 51, 5))

print fives    # [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

In this program we are building a game board that is 5 rows by 5 columns. We create the board using a loop that iterates over a range from 0 to 4…

for row in range(5):     # range(5) returns [0, 1, 2, 3, 4]

Notice there are 5 rows in all, their indexes as listed above. The first row is at index[0], the fifth row at index[4]. We learned about zero-indexing in the module on Lists and Dictionaries, as well as covered it in the module on Loops.

Each row in our board list is another list, also with 5 elements.

board.append(['O'] * 5)

At the completion of the loop, our list looks like,

[['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O']]

Notice there are five lists inside a list? If we wish to see them as the row each represents, we can do it with a loop…

for row in board:
print row

which will output,

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

Of course, we will want to print the board more than once so it make sense to write a function that can be re-used again and again.

def print_board(board_in):
for row in board_in:
print row

Notice that this function does not use a range. The board is the iterable so we simply iterate over it. The number of rows and columns is already defined.

This brings us to the topic of the current lesson, Printing Pretty. We don’t wish our printed board to look like this,

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

but rather as this:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O

We get that output with,

def print_board(board_in):
for row in board_in:
print (' '.join(row))

print_board(board)

Notice that we passed in the global variable, board but named it board_in in the parameter, which variable is used locally by the function.

That brings us to the end of this example. If you are still not clear on what we’re doing, then follow the advice in my opening statement… Go back to the beginning of the track and do it over.

#3

Thank you so much for your help. I really appreciate you taking the time to put all that and it taught me a lot.

Do you by any chance recommend any books/websites that cover the basics? The way you explained everything was exactly what I need but feel like it’s just zoomed over in all the books or sites I look at. I am constantly getting confused about syntax and my eyeballs starts to hurt when it comes to multiple nesting or what is pulling from what.

#4

This site covers the basics, and sets the stage for further learning. How much is actually learned here depends upon the learner, but quantitatively it is still just a basic introduction. We all learn in different ways. One thing that cannot be disputed, though, is the value of repetition. That is why it is so important to go over the lesson material at least twice. On the second pass take the time to research each concept in detail, no matter how trivial it may seem. You should be able to do it in your sleep once the basics are drilled in.

Check the Corner Bar for a couple of recent posts on Python. They point to resources that you can bookmark and keep handy while studying. Also, get familiar with the Python documentation and learn how find things and understand how the syntax is explained.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.