15/19 Please help


#1

hello Here is 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

for turn in range(4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print "Turn", turn + 1

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)

But the outcome shows this:
Guess Col: 2
Turn 1
Guess Row: 1
Guess Col: 1
Turn 2
Guess Row: 1
Guess Col: 1
Turn 3
Guess Row: 1
Guess Col: 1
Turn 4
You missed my battleship!
O O O O O
O X O O O
O O O O O
O O O O O
O O O O O
None

What is wrong?


#2

Please, paste your code with indentation. Paste it, select it and press Ctrl + Shift + C or what's better, put your code between three backquotes/backticks before your code and three backquotes/backticks after your code.


#3

Sorry for causing you trouble but I figured it out myself, thank you :smile:


#4

No problem at all :smile: it's always better to find solution by yourself. However please remember about posting code with indents. It's much easier to find problem.
Good luck.


#5

Well, for those that may have problems with this exercise in the near future, take a clue:

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

Thank me later :grin:


#6

Santo it needs to dislplay Game Over though. How do you do that?


#7

How can I do in the board?