14. Test Run


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/5?curriculum_id=4f89dab3d788890003000096


When I type in two numbers in the range(i.e., 2, 3), it accepts my code. However, it rejects it when I put in numbers out of the range(i.e., 53, 23) and displays the following error message: Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range


Below 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) 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)


Any help would be very appreciated.


#2

Your program crashes on the line after "you missed my battleship", because the column and/or row is out of range.

board[guess_row][guess_col] = "X"

This means that your check for valid row and/or column should be done earlier. You could swap the two blocks of code:

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)

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)

Or make it even simpler by adding your "sank my battleship" check as an extra "elif" statement.

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."
elif (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)

#3

Thanks, it solved my problem. :slight_smile:


#4

I have one more thing. I just realized when I simply hit enter, another error message appears, saying Traceback (most recent call last):
File "python", line 23, in
ValueError: invalid literal for int() with base 10: ''
Do you know how to fix this?


#5

This is because the input is immediately converted to an int, while the input might not be an integer.

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

You can use the method "isnumeric()" to check for a numeric string. Taken from the Python docs (https://docs.python.org/3/library/stdtypes.html).

So in short: there must be at least one character and the whole string must be numeric.

guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")
if (not guess_row.isnumeric() or not guess_col.isnumeric()):
    print "The row or column is not a number!"
elif ...

#6

Thanks again for all of your help. Since it told me to do the integers, would it probably be best if I left it as is?


#7

The code can probably be left as it is, but I believe the course automatically 'replaces' some of it in the next exercises (so you will never start off with broken code).


#8

@quickbreakdown no it does not do that.


#9

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