Game Over -- Please help!


Please guys, I think I did everything right but the game simply doesn't stop after a "Game Over" and brings out an error :cry:

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!"

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(5):
    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!"
        print "Turn", turn + 1
        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."
            print "Turn", turn + 1
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            print "Turn", turn + 1
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        print "Turn", turn + 1
    if turn == 3:
        print "Game over"



You do not need to code print "Turn", turn + 1 after every if, else,elif.
Also you said print board but your if turn ==3; is after that.

The instructions are -

Add an if statement that checks to see if the user is out of guesses.
Put it under the else that accounts for misses.
Put it after the if/elif/else statements that check for the reason the player missed. We want "Game Over" to print regardless of the reason.
If turn equals 3, print "Game Over".

In coding we try not to have repetition of code. If you place it correctly you do not need to repeat.

Hope that helps.


the print of game over should be an exact match both Game and Over should have a uppercase first letter



Nice, I did not spot that. 'Game Over'.:grinning:


A post was split to a new topic: Game over help


you used range(5) instead of range(4)


thanks! I got it now.


i can't believe i missed this.. thanks