Help me with the battle


#1

anybody who has finished this lesson, pls tell me how to code you win and bad aim
what does the next step 'not again' mean?


#2

Please post your code


#3

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

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print 'Congratulation! You sank my battleship!'
else:
if guess_row not in range(len(board)) or \
guess_col not in range(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)

at bad aim step
program just keeps on asking me to make sure i print the "Opps..." if the user guess is off the board


#4
if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sank 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_board(board)

Your if and else block should be formatted like this.

Note how on this line I have checked for the boards limits

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

This is where you were receiving your error.


13. Not Again!
#5

The valid values for guess_row are 0 to rows-1 and the valid values for guess col are 0 to cols-1. You will need to build up a complex Boolean expression of the form:
this is the hint in the instruction
i wrote the code following this note


#6

I have used that exact same code and it executes but still tells me I need to add the elif for it to print you guessed that already?

What gives? could use some help


#7

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(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."