Battleship error in Python 3.4


#1



Hi everyone! :slight_smile:

I've just finished the "Battleship module", where this code ran perfectly. However, when i passed it to my Python 3.4, i noted some lack of information in the game.


First, in every turn, the board does not print the guessed column and row (denoted by X). Also, if i guess the same column and row that i've guessed in a previous turn, it only prints "You missed my battleship."


There is some misleading logic that i can't see in my Python file and i'm really struggling about it, once that in the Codecademy interface, in every case, this code ran any logic path!

Thank you very much for your attention!

My coding regards :wink:

This is my code:

from random import randint

board = []

for x in range(1,6):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print (" ".join(row))

print ("Let's play Battlefield!")
print_board(board)

def random_col(board):
    return randint(0, len(board) - 1)

def random_row(board):
    return randint(0, len(board) - 1)

ship_col = random_col(board)
ship_row = random_row(board)

for turn in range(1,4):
    guess_col = int(input("Guess Col: "))
    guess_row = int(input("Guess Row: "))

    if guess_col == ship_col and guess_row == ship_row:
        print("Congratulations! You sunk my battlefield!")
        break

    else:
        if (guess_col < 0 or guess_col > 4) or (guess_row < 0 or guess_row > 4):
            print ("Oops, that's not even in the ocean.")

        elif (board[guess_col][guess_row] == "X"):
            print ("You guessed that one already.")

        else:
            print ("You missed my battleship!")
            board[guess_col][guess_row] == "X"

    print_board(board)

    print ("Turn", turn)

    if turn == 3:
        print ("Game Over")


#3

this line:

        else:
            print ("You missed my battleship!")
            board[guess_col][guess_row] == "X"

you should update this coordinate with X, which means you assign a new value, which is done with a single equal sign, double equal sign means compare


#4

Yeah, it fixed the problem.

Thanks stetim94 !!


#5