Battleship extra credit .format question


I've been trying to create a customizable battleship game where the player gets to choose the board size and the difficulty. I have tried to use the {}.format instead of %d to modify the board size with the raw_input, but it seems to spew out errors that I could not understand why it wouldn't work. The code so far is:

from random import randint
"""import the random # creator
board = []

bs = int(raw_input("Choose the board size"))
for x in range({}).format(bs):
board.append(["O"] * {}).format(bs)

def print_board(board):
for row in board:
print " ".join(row)

Can anybody help me how to replace certain part of the code with the player's raw input?
Thanks in advance.


board is a list.
You're calling the append attribute on board
list.appendhas no return value (None)
None doesn't have an attribute called format, but you are trying to access that.

There's a string method named format, it formats strings. But what are you trying to do? It looks like you're trying to create a list, not format a string.


I thought the format command would do something similar to %d or %s after a short google search as the code of something like
For x in range(%d) %(board_size):
Blah blah blah
However this format didnt give me the expected results


It would, but str.__mod__ (that's what % uses) doesn't create lists either. And you were trying to use None.format instead of str.format

Also, strings are not code. You can't create a string that is code and run it.

Well, actually you can do that as well, but why not just write the code?

If you need to change what you're multiplying by, then use a variable


If you want to do something bs amount of times, then you'd do this:

for n in range(bs):
    # do stuff `bs` times