14. Test Run


#1




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


print "Oops, that's not even in the ocean."


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


#2

here:

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

you update the board before checking if the shot was in the ocean, you should update the board here:

    else:
        # update baord here
        print "You missed my battleship!"
        print_board(board)

after you checked the shot is in the ocean and the user hasn't already guessed it


#3

That seems to make sense, thanks for helping me out!


#4

Aside from that, does it matter on what level the print_board(board) is indented?


#5

you are going to add a loop now, so the question is what you want. if i read the exercise correctly, you can do this: (method one)

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

you can also do: (method two)

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

this way, each iteration (after you added the loop) of the loop, the board gets printed again. The exercise seems to want you use method one, but both is fine.

method one: the boards get only printed again if the user enters coordinates which are in the ocean and not guessed yet, method 2 will print a new board in all cases except for the user guessing your ship

The exercise seems to choice method one, because that is the only time the board gets update.

There are pro's and cons to both methods


#6

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