Bad Aim - Keep getting false passes


#1

I keep getting false passes, in too many ways to list them all. So I'm just going to post the current one.
If you look at the image below, you can see a couple things happening that shouldn't.

One, the computer picked 0,0. This suggests something went wrong in a different exercise, so something passed me through as it is that shouldn't have. (edit: or i'm misunderstanding the output, of ship_row and ship_col, which is entirely possible)

Then, 5, 5 is considered not in the ocean. I am not even sure how many things I've tried to make this work, and every time I get a pass from the course. I have hit my frustration tolerance and then some.

Below is the current incarnation. I've been doing different things so it's likely that I made the code worse at some point and I can't even see it anymore.


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:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

print ship_row
print ship_col

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else: 
    if guess_row is not range(5) or guess_col is not range(5):
        print "Oops, that's not even in the ocean."
    else:
        print "You missed my battleship!"
        board[guess_col][guess_row] = "X"


#2

Hi, @rampion.rampage ,

With five rows and five columns, they are each indexed from 0 to 4. So, 0,0 is a valid location, and 5,5 is not in the ocean.

However, this statement needs to be revised ...

if guess_row is not range(5) or guess_col is not range(5):

Consider not in instead of is not.

Here, you do not need to subtract 1 from the user's guesses ...

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

#3

The subtraction was one of my many failed attempts (as in, it wasn't originally part of what I had before going crazy trying to solve the problem).

Works fine now, except that most people would input 1,1 for the top left and 5,5 for lower right, but this game is only organized for 0-4.
Which is why I added the parenthetical comment that it's likely just how the code is at the moment. But generally that is now how one makes guesses in this game.


#4

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