14. Test Run Oops too often?


#1




When I type any col or row, it always says "Oops, that's not even in the ocean." EVEN when I type the numbers 3 and 5


I expected it to say that I missed, or that I hit, also, when I type in the correct ship coords, it says that I hit, idk what's happening.


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)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))



# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    
elif guess_row != range(5) or \
  guess_col != 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)


#2

5 is not among 0 1 2 3 4


#3

?? what do you mean?


#4

That 5 is outside the board


#5

I used any of 0 1 2 3 4 or 5 and it still said oops


#6

You just listed 6 different numbers, but the board is only 5 by 5. One of them is outside the board.


#7

Your description of the bug does not describe a bug because you're using numbers that are outside.

If it's also behaving incorrectly for coordinates that are inside the board, then that would be something that I agree with you to be a bug.

And to start approaching that bug, we'd have to begin by correcting the bug description, otherwise we would be chasing something that isn't there.


#8

Let's say that you're still getting told it's outside for the coordinates 2, 2

That would be a bug in the part of your code that determines whether it should print that message or not. That's a single line, so that narrows down where the problem is by a lot. So what you can do next is to study that line, consider why you wrote it the way you did and think about whether you still think it looks like it should work. You can try to come up with alternative ways of doing it. You can investigate closer what it's doing .. and so on.


#9

thx, and also, you're correct, it does do that


#10

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