#1

The code below passes the lesson when In the console I write numbers between 0-4 but when I write 5, instead of saying “Oops, that’s not even in the ocean.” it bugs out and the error in the console is:

the 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:
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 the ocean."
else:
print 'You missed my battleship!'
board[guess_row][guess_col] = 'X'
print_board(board)
``````

help please!!! I passed the lesson but the code isn’t really working…

#2

Which indexes are valid for your board? Is 5 a valid row/col?

#3

no. its 0-4. when I do 5 in the input it’s and error and not saying “Oops, that’s not even in the ocean.”

#4

Then you’ll need to check if it’s in that range before using it right?

#5

line 37 board[guess_row][guess_col] = ‘X’

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

I am not sure why it is wrong, help?

Here is the 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 ship_row == guess_row and ship_col == guess_col:
print “Congratulations you sank my battleship!”

else:
if guess_row not in range(5) or guess_col not in range(5):
print “Opps, that’s not even in the ocean.”

``````else:
print "You missed my battleship!"
``````

board[guess_row][guess_col] = ‘X’
print_board(board)

(Not pasting exact indents like in my example)

#6

``````dsdf
``````

you need to put 3 backticks (`) before and after the code. its really not comfortable seeing the code like this.
btw about my problem, they fix it and put a loop in lesson 14 I think

#7

this is the code i used that worked fine, I also used elif instead of creating another if statement like it said on the instructions:

``````# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
elif 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)
``````

#8

jaavv’s is right about the solution he gave. I will try to explain what is happening the best I can, so you understand what is happening

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’

Notice that if you set guess_row = 5 and guess_col = 2, that will obviously be a miss; hence, the else statement is executed. “You missed my battleship!” will print, but board[5][2] will throw an error because board[5] doesn’t exist; board[4] is the max value. The key is to prevent out-of-range values from being executed. You can achieve this by testing out-of-range values right when there is a miss. As seen below, only a “Oops…” print statement is executed, and board[5][2] will never be evaluated. Just don’t forget to reprint the board after the “Oops” print statement, so the game can continue :).

Recommended code, courtesy jaavv:
if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”
elif guess_row not in range(5) or guess_col not in range(5):
print “Oops, that’s not even in the ocean.”

Hope this helps

#9

Sorry for the wrong indentation. Formatting issues.

#10

This is 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 & 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 the ocean”
else:
print “You missed my battleship!”
board[guess_row][guess_col] = ‘X’
print_board(board)

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.

#11
``````elif   guess_row not in range(len(board)-1) or guess_col not in  range(len(board)-1):
print "Oops, that's not even in the ocean."
``````

when guess_row =5 and guess_col=5 . Message: “Oops, that’s not even in the ocean.”

the same message if guess_row =5 and guess_col=5 and code written below

``````elif   guess_row not in range(len(board)) or guess_col not in  range(len(board)):
print "Oops, that's not even in the ocean."
``````

and if guess_row =5 and guess_col=5 and code written below

``````elif   guess_row not in range(len(board)+1) or guess_col not in  range(len(board)+1):
print "Oops, that's not even in the ocean."
``````

error:

list index out of range

Help me please to make this program more flexible

#12

Thank you… that’s what I wanted to do initially, but tried it their way assuming I was wrong… Now I can continue.

#13

I have made some changes to my code.
I have created variable max_v=5

now my code is

``````from random import randint

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

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:
print "Congratulations! You sank my battleship!"
elif   guess_row >max_v  or guess_col >max_v:
print "Oops, that's not even in the ocean."
elif board[guess_row][guess_col] == 'X':
print "You guessed that one already."

else:
print "You missed my battleship!"
board[guess_row][guess_col] = 'X'
print print_board(board)
``````

and condition verification of “Oops…” is more flexible. But now it shows me an error on condition verification of “You guessed that one already.” . Error is :

IndexError: list index out of range

and it makes me crazy

#14

@arcninja56229

You never adjust your player input, if you enter 5 you are telling it the 6th index location. So just adjust your player input to match what the computer wants.