11. Danger, Will Robinson


#1



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
Guess Row: 1
Guess Col: 2
3
0
You missed my battleship!
Traceback (most recent call last):
File "python", line 34, in
TypeError: 'int' object has no attribute 'getitem'


I'm trying to get the spot you guessed to be marked as X, and to allow the user to attempt to guess where the battleship is more than once using the if and else conditional commands.


from random import randint

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

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

print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    
else:
    print "You missed my battleship!"
    print ship_row["X"][0]
    print ship_col[0]["X"]
    print_board(board)


#2

Hi @ethanpui,

This will not work ...

    print ship_row["X"][0]
    print ship_col[0]["X"]

To mark the spot that the player guessed as "X", use guess_row and guess_col, as indexes, as follows ...

    board[guess_row][guess_col] = "X"

#3

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