Just a question


#1

So, I was improving on the battleship game from the Python tutorial and porting it to Python 3, when this lovely showed up:

Traceback (most recent call last):
File "C:/Users/[DATA EXPUNGED]/battleship.py", line 39, in
board[guess_row,guess_col] = 'X'
TypeError: list indices must be integers or slices, not tuple

Code (Buggy line in quote):

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)

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)

Everything from here on should go in your for loop!

Be sure to indent four spaces!

for turn in range (4):

guess_row = int(input("Guess Row:")) -1
guess_col = int(input("Guess Col:")) -1

if guess_row == ship_row and guess_col == ship_col:
    print ("You sunk it. Hooray.")
    print ("It was at %s, %s") (ship_row, ship_col)
elif guess_row > 5 or guess_row < 1 or guess_col > 5 or guess_col < 1:
    print ("You are bad at life.")
else:
    print ("You missed!")
   >  board[guess_row,guess_col] = 'X'
# Print (turn + 1) here!
print ("Turn %s") % (str(turn + 1))
print_board(board)

#2

Hello, @arraysurfer21398!
I get that you tried to put the string "X" at position (guess_row, guess_col) but this is not how it really works.
The thing you should've done, and which is presented to you in earlier lessons on the Battleship section, is:

board[guess_row][guess_col] = "X"

Regardless if is Python 2xx or Python 3xx, it's supposed to work.
I'd suggest you to give it a try :slightly_smiling:

Quote me if there's anything else I can help you with!

Best regards,
g4be