Bad aim


#1

I did , but still have problem with line 33 :slight_frown:
Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range

This is how I changed 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

if guess_row == ship_row and guess_col == ship_col:# Write your code below!
print "congratulations! You sank my battleship!"
else:
if guess_row != ship_row and guess_col != ship_col and guess_row in range(5) or guess_col in range(5):
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 the ocean."
else:
print"You missed my battleship!"
board[guess_row][guess_col] = 'X'
print_board(board)

what I cant understand is I passed this line with the same code before and line 39 is just like line 33.so why havent this error for line 39 or how did code cadeny let me pass?


Bad aim , really need help
#2

Hello, @rmbat1380 and @glucidi ,

@stetim94's comment is the key to understanding what you need to do here. A well-written program should be designed to catch and handle a user's input to make sure it is valid instead of allowing that input to be processed as if it were valid. If the input is not valid, the program should provide the user with guidance, rather than let the program crash and issue a message that contains technical programming jargon. This is a universal concept to keep in mind whenever you write a computer program.

If the user enters invalid coordinates in the Battleship program, and then the program attempts to use those coordinates to access the board list, we have observed that this will throw an error, and issue the following technical message ...

IndexError: list index out of range

Therefore, we need to structure our if-else blocks so that they catch the user's invalid input, and display this message instead ...

Oops,that's not even in the ocean.

... without allowing execution to proceed to a point where it attempts to use the coordinates to access the board list.

Revisit the instructions, and follow them carefully to build the proper nesting of the conditional blocks.


#3

ok thank you :heart:


#4

Hey Im so sorry for being such an idiot but I change it again ,went to the previous section and try again so right now when I try sth out of board or sh wrong inside board it seems like I did it right caues the console just show what it should but I have this error:

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.

and this is what the console shows:

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: 5
Guess Col: 7
4
2
Oops,that's not even in the ocean!
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
None

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

if guess_row == ship_row and guess_col == ship_col:# Write your code below!
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 the ocean!"
    print_board(board)
else:
    print"You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

#5

Hi @rmbat1380 ,

Make sure that the message you output matches exactly what is specified in the instructions. You have ...

Oops,that's not even in the ocean!

Every character needs to match. Compare the above with the specifications in the instructions.

It is important to format all your code when you post it. Correct indentation is essential in defining the flow of control in Python programs, and we need to see the structure of your code in order to understand what it does. See How do I format code in my posts?.


#6

thanks I just passed it, yup I think I had problem in "Oops, that's not even in the ocean."
:heart:


#7

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