11. Danger, Will Robinson! Board after guess outputs X in wrong position



If I input my code as is, when it prints the board after my guess, the 'X' is in the wrong coordinate position than someone would expect if they were playing, giving (x+1,y+1) .

Yet, if I modify the line:

board[guess_row][guess_col] = 'X'


board[guess_row - 1][guess_col -1] = 'X'

it gives me the correct output on the board when it is printed after the guess, but tells me > "Oops, try again. You're updating 'guess_row - 1' and 'guess_col - 1' instead of 'guess_row' and 'guess_col'." I know that the random ship is placed according to it's index position in the row, but I'm trying to work on the assumption that whoever is going to play would use the coordinates (1-5) rather than the index position (0-4) because common sense. I believe i want to change the result of guess_row so that it is -1 whatever the raw input is, but I don't know how, and I also believe this would mean that the line

if guess_row == ship_row and guess_col == ship_col

would mean that even if the position on the printed board was correct, the code would mean that the 2 aren't aligned properly so would return a miss? I hope i've explained this well enough for someone to offer their help! The code that i have that it tells me is correct is:

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)


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

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    print "You missed my battleship!"
    board[guess_row][guess_col] = 'X'


1-5 makes no more sense than 0-4, it's just labels and you don't need to label everything the same way

Having -1's all over the place is a huge mess, either change the lists so that those locations exist or translate for the user

Easier to just stick to the described design when there's automated testing involved


Hi @ionatan ,
I wanted to know if the X in the else statement is supposed to be spot that the user entered.
It so, why is it printed incorrectly ?
If not, what is it supposed to be ?

Also if you could point me to some clear reading material about Python functions and placeholders. I still can't fully understand it.


Nevermind, I got it. Even OP did a good job explaining what was happening. Sorry for rushing.
Thanks, @ragingblasian


I do get that, at least from a coding perspective, 0-4 makes more sense. But my thought is that anyone who doesn't do the coding would just expect it to start at 1, like any board game does (including the actual game!).

It doesn't matter so much though, I completed the topic, and can just tinker in the Extra Credit to get what I want.


It took me a while to understand placeholders. Every time the course told me to use one I was like "But what does this mean, it's not defined anywhere!" and then I realized that they're just replaced by the value in the list that the loop is working on.

It is rather frustrating though that this isn't clearly explained early on though.


Yes, that's what I felt for a long time and I still feel it to some extent. There's some kind of intuitin to Python that's still counter-intuitive to me.


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