Play It, Sam: Something wrong with provided code? (2 issues)


#1

Link to Exercise:

Incorrect Behavior(s):
Issue 1: When entering coordinates that should fall into the specific range of the code, the response is "Oops, that's not even in the ocean." instead of "You missed my battleship!"

Issue 2: If you enter coordinates outside of the specified range, you get a python error saying

Traceback (most recent call last):
  File "python", line 42, in <module>
IndexError: list index out of range

And an error message: Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: list index out of range

Expected Behavior:
Issue 1: I would hope to see the 'You missed my battleship!' message.
Issue 2: When entering coordinates that are out of range, the "Oops, not in ocean" message displays and no error from the script.

Code:

from random import randint

board = []

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

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        if (guess_row or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            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 "Turn", turn + 1
    print_board(board)

What I get:

  • Issue 1 -

All three guesses are guesses for coordinates that DO exist on the board, but I get the message claiming they don't.

Let's play Battleship!
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
0
4
Guess Row: 3
Guess Col: 2
Oops, that's not even in the ocean.
Turn 1
O O O O O
O O O O O
O O O O O
O O X O O
O O O O O
Guess Row: 3
Guess Col: 4
Oops, that's not even in the ocean.
Turn 2
O O O O O
O O O O O
O O O O O
O O X O X
O O O O O
Guess Row: 4
Guess Col: 1
Oops, that's not even in the ocean.
Turn 3
O O O O O
O O O O O
O O O O O
O O X O X
O X O O O
Guess Row: 1
Guess Col: 2
Oops, that's not even in the ocean.
Turn 4
O O O O O
O O X O O
O O O O O
O O X O X
O X O O O
None

  • Issue 2 -

Let's play Battleship!
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
4
2
Guess Row: 5
Guess Col: 5
Oops, that's not even in the ocean.
Traceback (most recent call last):
File "python", line 42, in
IndexError: list index out of range

Note:
While I didn't write this particular code, it's almost entirely like the code I did write for the previous exercise. The only difference is that I wrote my code to check if guess_row or guess_col were >= 5 for the 'oops not in ocean' statement. I have tried altering the code to what I wrote to see if that would fix things but it doesn't so have since converted it back to what was provided by code academy.


#2

when comparing with a or, comparison should be done at both sides:

guess_row < 0 or guess_row > 4

besides, your code wasn't even checking if the value was lesser then zero.

that should fix issue 1, and also issue 2, given your function now correctly handles false input


#3

Thanks stetim94!

I didn't undo enough and forgot about the < 0. That did fix issue 1.

I still get issue 2 though. Any other corrections?


#4

this two lines:

board[guess_row][guess_col] = "X"
print "Turn", turn + 1

should be part of else:

else:
     print "You missed my battleship!"
     # update board and turn here

you only want to update the board if the user enters a valid (in the ocean and not guessed before) option.

Also, you might want to insert a break:

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break

this way, the loop will stop if the user sank your ship


#5

Gah indentation! Okay, that resolves those issues. I hadn't considered adding a break either.

Thanks for all of the help/clarification!


#6

you're welcome, good to hear everything is resolved now


#7