Test Run


#1

Hello, so everything works fine but when I guess numbers higher than 5 i get this error:
File "python", line 31
elif board[guess_row][guess_col] == "X":
^
IndentationError: unindent does not match any outer indentation level

My code:


#2

the "^" is pointing at the : in the end of the "elif board[guess_row][guess_col] == "X":(here)"


#3

Seems to me like you have got mixed tabs and spaces. Just delete all your indentation and use either four spaces or tab character only.

Or, just paste your code here using proper formatting - discuss.codecademy.com/t/using-backticks-to-format-your-code/3697/2

Thanks!


#4

hmm i cannot click on the link


#5

Odd. now just copy and paste the link then. should work.


#6

this is the full code now I get different error this time its:
Traceback (most recent call last):
File "python", line 31, in
IndexError: list index out of range
line 31 is this one: elif board[guess_row][guess_col] == "X": the same one

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"
elif board[guess_row][guess_col] == "X":
    print "You guessed that one already."
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 "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

BattleShip Test Run!
#7

if guess_row not in range(5) or \
       guess_col not in range(5):

this is the condition for checking if the index is out of bounds of the board list and this should be before the statement:

elif board[guess_row][guess_col] == "X":

Can you guess why? (note that you get an error on the above very line, when the input exceeds the bounds)


#8

yea that makes sense, the last two lines on the code shouldn't be there too they should be before the range, right ? At least that's how it works now


#9

No. the last three lines:

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

should be left as is (inside the else) because you first need to check if the user's guess matches an existing guess (refer elif). If it doesn't (else), you will mark the new guess as X and tell the user about it (print) and print the new board layout. The general plan is:
User input:

  1. matches ship coordinates, else
  2. exceeds board coordinates, else
  3. matches existing guess, else
  4. mark new guess, give user another chance to play

Hope it helps! :smiley:


#10

yes it did help, thank you :slightly_smiling:


#11

now when I try it I get the : IndexError: list index out of range for the 2nd to last line, this one board[guess_row][guess_col] = "X"

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

Edit: I just forgot to put it under the 2nd else my bad i'm okay now, I think


#12

I dont get why i am getting that error could you assist me please


#13

Remove the else block from line 31-34.


#14

It's ok now:

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:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."

elif board[guess_row][guess_col] == "X":
    print "You guessed that one already."

print "You missed my battleship!"
board[int(guess_row)][int(guess_col)] = "X"
print_board(board)

#15

@gaurangtandon my if statement inside else statement, is not working whenever i enter value > 5 in guess_row and guess_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 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_board(board)

#16

You should check if guess_row/guess_colis not in range BEFORE assigning"X" to the board.