# 19. Extra Credit - Help Requested!

#1

I have come back to my Battleship! program upon completion of each of the Codecademy Python lessons and am attempting to add another ship to the game.

Here is my code:

``````from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print "Let's play Battleship!"
print_board(board)
class Ships(object):
def __init__(self, ship_row, ship_col, state):
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board) - 1)
self.ship_row = ship_row
self.ship_col = ship_col
self.state = state
ship_A = Ships(randint(0, len(board) - 1), randint(0, len(board) - 1), 'active')
ship_B = Ships(randint(0, len(board) - 1), randint(0, len(board) - 1), 'active')
for turn in range(7):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
def sunk_ship_A(guess_row, guess_col):
if guess_row == ship_A.ship_row and guess_col == ship_A.ship_col:
ship_A.state = 'sunk'
print "Congratulations! You sunk a battleship!"
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
def sunk_ship_B(guess_row, guess_col):
if guess_row == ship_B.ship_row and guess_col == ship_B.ship_col:
ship_B.state = 'sunk'
print "Congratulations! You sunk a battleship!"
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print "Turn", turn + 1
print_board(board)
if ship_A.state == 'sunk' and ship_B.state == 'sunk':
print 'Congrats! You sunk the battleships!'
break
elif turn == 6:
print 'Game Over!'
ship_row = str(ship_row)
ship_col = str(ship_col)
print 'The battleships were at: %s, %s' % (ship_row, ship_col)``````

When the code is run, an error is returned through the console. The error is as follows:

``````Traceback (most recent call last):
File "python", line 10, in <module>
File "python", line 16, in Ships
NameError: name 'self' is not defined``````

As of my understanding, the argument 'self' gives the objects that 'Ships' creates it's identity. In turn, it does not need to be defined; it's definition is the name of the object being created. Why does this error occur?

#2

hey, I did not get to objects yet, so not sure how to help you (but I guess you must move your lines in error first line after you define a new class - but that is my guess only), but I have another question as I was expanding my battleship game too - without objects however. My question is - how do you check when creating ship_A and ship_B ships that the second one does not get created on the same spot as the first one (line 15 and 16)?

#3

I don't have an answer to your question; I haven't gotten to checking the ship positions and readjusting the positions if they spawn on top of each other. I was going to cross that bridge when I get there.

Regarding your suggestion for my class - I did not know exactly what you meant, but I had removed the methods from my class and it worked!

#4

My initial though is to put the coordinates of each ship in another array and then upon creating new ships, reference the coordinates in that array, and if they are equal to any set in those, remove them and run the ship creation again