Play It, Sam


#1



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
Turn 1
Guess Row: 3
Guess Col: 4
Turn 2
Guess Row: 2
Guess Col: 3
Turn 3
Guess Row: 2
Guess Col: 3
Turn 4
Guess Row: 23
Guess Col: 3
Turn 1
Guess Row: 4
Guess Col: 1
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O X O O O
Turn 2
Guess Row: 5
Guess Col: 2
Oops, that's not even in the ocean.
Turn 3
Guess Row: 3
Guess Col: 1
You missed my battleship!
O O O O O
O O O O O
O O O O O
O X O O O
O X O O O
Turn 4
Guess Row: 2
Guess Col: 4
You missed my battleship!
O O O O O
O O O O O
O O O O X
O X O O O
O X O O O

At first, the first four turns does not end, and does not follow the rest of the code. Even though there are only four turns, the code repeats and gives eight turns instead. The second half of the console seems fine, but what is wrong? Here's my 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)
for turn in range(4):
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
ship_row = random_row(board)
ship_col = random_col(board)


# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    print "Turn", turn+1
    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 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 guess_row==board[guess_row] and \
            guess_col==board[guess_col]:
            print "You guessed that one already."
        else:  
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
            print_board(board)


#2

This does not look quite right. We should be looking for an 'X' at (row, col).

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

Aside

Just a word on the control flow... Consider.

    if __:

    elif __:

    elif __:

    else:

In oher words, turn this,

    else:
        if __

into,

    elif __:

#3

You have two loops in code with "Trun".
You have to delete first loop.


#4

When the A, B, C of economics is applied to this situation we can deduce that the success rate is marginally low.

5% succeed 85% of the time
10% succeed 10% of the time
85% succeed 5% of the time

What this suggests in terms of success is that,

85 in every 100 have a 1 in 20 chance;
85 / 100 * 1 / 20  =>  85 / 2000  => 17 / 400

10 in every 100 have a 2 in 20 chance; and,
10 / 100 * 2 / 20  => 20 / 2000  =>  4 / 400

5 in every 100 have a 17 in 20 chance.
5 / 100 * 17 / 20  =>  85 / 2000  =>  17 / 400

That's 95 people per 1000.