12/19 lesson passing, but when I enter something specific, gives me wrong output


#1

Hello,

Here is my code:

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_row][guess_col] = "X"
    print_board(board)

If my answer makes it so guess_row = ship_row and guess_col = ship_col, then it prints out “Congratulations! You sank my battleship!” which is fine, then when my answer is not in the range it prints out “Oops, that’s not even in the ocean.” which is fine, BUT when I write the wrong answer, but its within the range, it doesn’t print “You missed my battleship!”, it prints “Oops, that’s not even in the ocean.”

Thanks!


#2

You’re comparing identity between guess_row and range(5), and they will never be the same beacuse range(5) creates an entirely new value which guess_row can’t possibly already refer to

range(5) is range(5)  # False, these are two separate lists
True is True  # True, because only one "True" exists
'' is ''  # Probably True, Python is likely to re-use the same value
1 is 2-1  # Probably True, small integers are probably re-used
[] is []  # False, two separate values, they are not the same one
dict is dict   # True, referencing the same variable twice (same value both times)

#3

There is the operative keyword.


#4

So would I do instead of range(5), (0, len(board) - 1)?


#5

You’re missing the point. A coordinate is not a range, and even less is it the same object


#6

Ok, but I checked other problems to this and they all included range and the instructions even say it…


#7

Okay? But I bet they don’t suggest comparing identity.


#8

And also, the co-ordinate is putting the 2 numbers together? I don’t understand what you are trying to say.


#9

To specify a point in a plane (2D) you need two coordinates!

Consider this.

This water is that glass of water.

No, water is not a glass of water, and it isn’t that glass of water, the water and the glass have separate identities.

You might however ask whether there is water in the glass.

Test for membership not sameness


#10

Ok, I ask the user to input 2 NUMBERS. I don’t see how it comes to 2 co-ordinates. What are you suggesting I replace it with then?


#11

I’m not sure what you mean and I’m not sure it’s related at all either


#12

If you do this:

1 == [1, 2, 3, 4] # not even the same type

You will always get a result of false, because the left side is the number one, and the right side is a list. The types don’t match, they are obviously not equal, and they are also not the same values, they are two separate values.

1 is [1, 2, 3, 4] # no, not by a long shot

[] == [] # yeah, they are equal.
[] is [] # sure, equal, but not the same one


#13

The code I wrote worked, but I didn’t get the output I expected. It said this in the instructions:

Like the example above, it should check if guess_row is not in range(5) or guess_col is not in range(5).

And in what way am I comparing the identity? I still don’t understand, you are saying the same things over and over again but it isn’t telling me anything.


#14

This is the whole code

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) - 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!")
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_row][guess_col] = "X"
    print_board(board)

#15

It’s in your code.

That is never ever the case, that condition will always result in false.

edit* always results in true, since it’s got a not in there


#16

I still don’t know what I have to put there. And yes, I KNOW thats the line you are talking about!!!


#17

You’re not supposed to put anything in particular there.

You’re supposed to write code which carries out a particular action. How you write that code is up to you. You need to test whether a number is between two other numbers, you can write that in many ways and any one of them is what you’re “supposed” to put there.

It’s not about writing an exact piece of text. It’s about describing an action in any way you see fit.


#18

Use only things that you understand. Learn more things to understand more to write more.

That’s why the course starts by going over integers, + - * / % ==, assignment, loops, variables, and so on. Those are the things that you should know what they do, and they are what you should use to build everything else with, and each thing that you write should be something that you can argue in full for why they do what you want.


#19

Alright, I understand. I just thought I would always have to follow the instructions, but I solved the problem.

I did guess_row > 5 or guess_col > 5 and it works now. But just out of curiosity, why wouldn’t “is not” work? I still don’t really understand, sorry. Got recently introduced to is not.

Thank you for your help.


#20

Why would it work? What is there to back that up, what is the argument for why that would do what you want? There’s nothing for me to poke holes at there. And I did explain the difference between identity and membership.

You haven’t really been introduced to is or is not because you haven’t been introduced to the concept of identity. You don’t have this in your toolkit whatsoever, you can’t use it.