Bad aim


#1

I think i have followed all instructions in my code yet i am getting the error " Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range"

What am i doing wrong ?
Please help me
my code:
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!"
board[guess_row][guess_col]="X"
print_board(board)
if guess_row not in range(5) or guess_col not in range(5):
print "Oops,that's not even in ocean."
else:
print "You missed my battleship!"
board[guess_row][guess_col]="X"
print_board(board)


#2

The problem is with your if/else statement, you have the order mixed up a bit.

First, it should check if the user's guess it correct, (you have this part right).

Next, it should branch off into an else, which contains an if/else.
If the user's guess it out of range then you should print the error message.
Else, you should print "You missed my battleship!" and change the "O" to an "X".

You have to make sure the guess is on the board before you change it to an "X", but you changed it to an "X" before the if guess_row not in range(5) or guess_col not in range(5):. This means that if the guess was out of range, the program would try to change an "O" that doesn't exist to an "X".

Hope this helps! :smile:


#3

Hi , I made necessary changes. when I guess 8 & 9 for row and column respectively, it print out the statement "Oops ,thats not even n the ocean " and then gives me an error called :

Oops, try again. Make sure you print "Oops, that's not even in the ocean." if the user guesses a row or column that is off the board.

I dont understand why.

my code:

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) - 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:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops,that's not even in ocean."
else:
print "You missed my battleship!"
board[guess_row][guess_col]="X"
print_board(board)


#4

Try this,

if guess_row == ship_row and guess_col == ship_col:
print "Congratulation you have sunk a battleship!"

else:
if guess_row not in range(5) and guess_col not in range(5):
print "Oops, that's not even in the coean."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)


#5

I have tried every solution given even in other links related to this section .But every time it gives up the same error although it prints "Oops thats not even in the ocean".
Any suggestions what i must change to pass this code?