Danger, Will Robinson!


#1

.
Add an else under the if we wrote in the previous step.

Print out “You missed my battleship!”

Set the list element at guess_row, guess_col to “X”.

As the last line in your else statement, call print_board(board) again so you can see the “X”.Make sure to enter a col and row that is on the board!

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)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
board[guess_row][guess_col] = "X"
print_board(board)
print “You missed my battleship!”

im getting Traceback (most recent call last):
File “python”, line 31, in
IndexError: list index out of range

as a error can someone help


#2

As instruction said your last line of code should be first statement of else.then Set the list element at guess_row, guess_col to "X". and at the last line in your else statement you should call the print_board(board) .

now question about the error

IndexError: list index out of range

so it depends on your input for guess_row and guess_col what you type from your keyboard.Instruction already told about that Make sure to enter a col and row that is on the board!


#3

3 posts were split to a new topic: Stuck in this section


#7