12. Bad Aim (No one else has this issue?)


#1

Hi everyone!
I am honestly loosing my mind with this. I can't figure out whats wrong with my code, or is it a codecademy bug.
Below is my code. Everything seems to be fine, no matter how many times I check it.
EXCEPT: Every time I run it, no matter what changes I make, it prints out: "Oops! That's not even in the ocean!"

So why does it always print that, even though I clearly have "working" If Else statements there?
I tried it with others codes also, same output!

CHANGING

if guess_row != (0, len(board)) or guess_col != (0, len(board[0])):

to

if guess_row != (0, 5) or guess_col != (0, 5):
...does not make a difference either!

I'm stuck and very frustrated, I am basically begging for help!

__________________________.
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))

def random_col(board):
return randint(0, len(board[0]))

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

Write your code below!

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


#2

Oh, one more thing.
Codecademy lets me pass still!
So it makes it even more confusing.


#3

Try 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)

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!

for turn in range(4):
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, %s" % (turn + 1)
    print_board(board)

it works for me.


#4

Thanks for the answer!
A friend of mine explained it to me, once I managed to reach him.
I was trying to compare an integer to tuple(in this case list), that means that the result of the comparison is always False, no matter what the user input is. That is why it always returned "Oops! That's not even in the ocean."
My new lines of code are following:

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."
else:
print "You missed my battleship!"
board[guess_row] [guess_col] = "X"
print_board(board)
______________________.

Now it works like a charm!
Pretty silly mistake of me, but thats how to learn :smile:
Could've not figured it out by myself.


#5

i have the same code does not work for me.
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."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)


#6

I can't see the indentations, so its hard to say if that is the problem.
I still don't know how to add code with indentations here, so I can't help you with that problem. :\

Will it give you any explanation about the error?


#8

I'm going crazy too. This is my code (i hope don't be a problem it is in Spanish), i think is pretty similar to which orccufin wrote.It always says to me "Agua, no tocaste mi barco", no matter if I find the ship. And if I guess it is on the row 6, for example, it returns error. Why?!


#9

I think the full code could help to find the error here, at least I cannot see any problem in that part of the code.
Could u post the full code in a picture, so that indentations are visible just like you did there?


#10

This is the entire code, thank you again!


#11


And this is the same but without loosing your eyes haha


#12

Im having the same problem as you man :confused:
I dont know what to do, the codes is "ok", as long as i give it values within the board, when I test it with invalid values, i shows an error message:

Traceback (most recent call last):
File "python", line 32, in
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 24: ordinal not in range(128)


#13

It looks like line number 16 has one extra indentation?
Try correcting that, what happens then?


#14

Oh you are right, but what really happened is that the previous lines had less indentation, I have corrected that and I continue having the same problem when I guess the boat is not in the ocean (e.g. row 5); at least now it works well when I find the ship.

I think the problem is in the "elif" but i don't deduce what it is.


#15

Hi everyone,

indentation is key.

Here is my 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[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_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 not in range(5) or guess_col not in 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)

Hope this helps,

~NocturnalDweller~


#16

Hi, guys!

My code:


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

I receive an error - Traceback (most recent call last):
File "python", line 36, in
TypeError: object of type 'int' has no len()

Why? :frowning:


#17

To be honest, I don't know why it wont accept that, but I just know it doesn't because I tried it myself.

Also I don't know which line is number 36, but I assume its:
if guess_row not in range(0, len(ship_row) - 1) or guess_col not in range(0, len(ship_col) - 1):

My friend explained to me that I am (and your are now) trying to compare integer with tuple, and that is why the program doesn't understand it. I didn't fully realize what he meant, maybe I am able to do that later when I learn more, but it just wont
So just replace range(0, len(ship_row)-1) with range(5) on both in line 36, and you should be good.


#18

thanks, bud! it works, but i tried to a little more than asked in this exercise )


#19

This is my code and whenever I type i values that are not on the board, it says ''oops that's not even in the ocean'' but it also says
Traceback (most recent call last):
File "python", line 38, in
IndexError: list assignment index out of range

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

print ship_row
print ship_col

Write your code below!

if ship_row == guess_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."
else:
print "You missed my battleship!"

board[guess_row][guess_col] = 'X'
print_board(board)

Thanks in advance


#20

it does not work :rage::rage::rage::rage::rage::rage::rage::rage::rage::rage::rage::rage::rage::rage:


#21

@netcoder23840

What is your malfunction? Post your code and we may yet find it.