# Oops, that's not even in the ocean!

#1

If the row and col are guessed correctly the “Oops” statement also pops up. Any ideas why?

``````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: "))

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

print_board(board)
``````

Also once from task 14 to task 15. The code changes from above to:

``````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_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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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 sunk my battleship!"
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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 (turn + 1) here!
print_board(board)
``````

which is fine but it makes me think I’m doing something wrong etc.

#2

Note that no comparison is made to `guess_row`

#3

Could you elaborate on that please, I don’t see the difference in either cases.

#4

That should be plenty enough for you to investigate, test, and reconsider yourself
I’m not speaking in riddles. I flat out said what the problem is. You have to have some degree of mastery over what you write and be able to do the rest of the reasoning here.

#5

Fair point but I just don’t understand what you mean by your answer:

but there is a comparison made to guess_col?

#6

There’s not. Your code would work. You’ve shown it does not.

And you can write simpler code which only contains only a comparison like that, to show that it does or does not do that. You can also ask yourself what tells you that writing this causes a comparison to happen. What out of the things in that line has the behaviour to make that comparison happen?

Whenever you write something you have to leverage behaviour you know to exist. If you don’t, then it won’t do what you want, there’s nothing to support that.

#7

You have an expression involving some values and the operators `or`, and `not in`

Python is not an intelligence, it does not guess what code means, this would make it very difficult to write correct code, if the meaning was changed through guessing.
Instead, certain things are defined which can be leveraged by the programmer.
Operators like `or` and `not in` have some particular behaviour. What is that behaviour, and does that cause two comparisons to happen in your expression? It doesn’t. Either you expect a different behaviour or you expect the behaviour to be adapted to what you wrote. What you wrote is English, and yes, it does mean something because those operators do have behaviour defined for what you wrote, it’s not even a special case, it’s merely following precedence, evaluating the expression in the order defined by the language.
Part of that behaviour you already know, and it is that those operators take two values as input, do something, and produce a boolean result. This makes it impossible that it behaves the way you want. You’d have to repeat those operators enough times that each comparison is made. It’s like how the plus operator (+) adds two values. Not three. You can certainly write code which causes that operator to be used multiple times, but there’s no such code in that expression. And you shouldn’t expect it to do that!
But if you were hoping it did so, it would be something you could test. You could argue for why it would do this, and test that argument.

You may even have taken the fact that it doesn’t trigger a syntax error as confirmation that it does what you want. But there’s a whole lot of things it might possibly be doing.

Don’t assume things, and treat that as fact. Find out.

#8

“if guess_row or guess_col not in range(5):”

I had to state “not in” for guess_row too right? I assumed guess_row WOULD be assigned with guess_col in “not in” (if that makes sense?). I treated it like an and expression.

#9

yes, otherwise there is no comparison for guess_row

wrong assumption i am afraid.

There is a “old” joke for this, where a wife says to here husband (who is a programmer). Can you buy 1 milk? And if they have eggs, bring 6. When the husband comes home with 6 bottles of milk, the wife ask what happened. The husband said: they had eggs.

#10

Python does have a whole lot of special syntax for common operations. A lot of that is rarely used or even known about simply because it doesn’t exist in (most) other languages (a whole lot of it is borrowed, so most of it probably exists somewhere)

For example comparison operator chaining:

`0 < 1 < 3`

This will compare 1 to both 0 and 3. Without operator chaining it would compare 1 to 0 and then compare the result of that to 3.
Equivalent expression: `0 < 1 and 1 < 3`
It’s rather similar to what you tried to do… But to use it you should first know that it can be done, otherwise you shouldn’t assume it does that. But you could try to find out, and if it turns out that way, then go right ahead… Easier to learn them and leverage them as needed though, eliminates the guesswork.

#11

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