12. Bad Aim


#1

What is wrong with my code. The editor won`t let me pass.

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 "0ops, that`s not even in the ocean."
else:
print "You missed my battleship!"
board[guess_row] [guess_col] = "X"
print_board(board)


#2

Difficult to tell, as you have used no indentation.


#3

Hey, why not use proper indentation by: "first", selecting all the code lines(which you copied from the codecademy's code console) and then using the </> ,which is "pre formated text option" from the editing window menu.


#4

I think that comment is meant for @maxwellchewe.


#5

@d_weaver
Oh, yes! Sorry :slightly_smiling: just a mistake.


#6

@maxwellchewe
Hey, why not use proper indentation by: "first", selecting all the code lines(which you copied from the codecademy's code console) and then using the </> ,which is "pre formated text option" from the editing window menu.


#7

Where can I find the </>


#8

click edit, then if you see above this text box you type in a little bar of things appears, click the </> one 3 to the right of the big I


#9

I don`t see the edit button


#10


See the picture, attached!


#11

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 "0ops, that`s not even in the ocean."
        else:
            print "You missed my battleship!"
            board[guess_row] [guess_col] = "X"
            print_board(board)

#12

Everything is working well but I keep getting this silly message "Make sure you print "Oops, that's not even in the ocean." if the user guesses a row or column that is off the board. "


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

** See the difference, in the last line. You're not calling print_board(board) properly:**
** #Now this one should work.**


#14

Thanks everything is now working fine