Battle Ship Not Working


#1

Even though apparently I have passed the course, my battleship is not working. When I press save and sumbit on 18 I have one go and that's it. Then In scroll up and see 3 previus goes up there that I never did what has happened can you please help. I don't know what I have done wrong here is the code and what is showing up on the screen.

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)

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
for turn in range(4):
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    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!"
        
        if turn == 3:
            print "Game Over"
        board[guess_row][guess_col] = "X"
        print "Turn", turn + 1
    print_board(board)

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 2
You missed my battleship!
Turn 1
O O O O O
O O O O O
O O O O O
O O X O O
O O O O O
You guessed that one already.
Turn 2
O O O O O
O O O O O
O O O O O
O O X O O
O O O O O
You guessed that one already.
Turn 3
O O O O O
O O O O O
O O O O O
O O X O O
O O O O O
You guessed that one already.
Game Over
Turn 4
O O O O O
O O O O O
O O O O O
O O X O O
O O O O O


#2

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

where you prompt the user for input is outside the loop, it should be inside the loop


#3

Write this line above the two currently above it, and indent them, and it will work.


#4

thanks guys i changed it and it works.
now i dont know why this isnt working. Can you explain to me why its wrong so im not just fixing it and not learingng anything
thankyou

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 5
Oops, that's not even in the ocean.
Traceback (most recent call last):
File "python", line 44, in
IndexError: list assignment index out of range


#5

lists are zero index based? so the indexes are: 0, 1, 2, 3 and 4

it seems your board update:

board[guess_row][guess_col] = "X"

is wrongly placed, it should be after you print you missed my battleship


#6

thanks for the help its working now


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.