11/19


#1
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)

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    
board[ship_row][ship_col] = "X"
print_board(board)

My code seems to be marking where the location randomly generated is rather than the spot i type in. How do i fix this?


#2

Use this instead,

print guess_row
print guess_col

You are printing out the Random vars instead of the guessed vars. :wink:


#3

Thank you!! I also am getting another error:
It looks like your game does not change the O to a capital X for an incorrect guess.


#4

Change this to

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

#5

Hi,
you need to change this "board[ship_row][ship_col] = "X"" to "board[guess_row][guess_col] = "X".


#6

im so lost on this part:
03.Set the list element at guess_row, guess_col to "X".

This is my code, where i'm i going wrong?

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

this is what i get back:

Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range


#7

Would you mind posting all of the code here?

I passed this without any bugs after I deleted the negative 1 (-1) from end of both "randint" lines.
(I just noticed many has used -1 there, for some reason. First I used that, but later I wanted to fix the code even that I passed the excercise)

So instead I added -1 to the end of quess_row and quess_col lines, like so:
guess_row = int(raw_input("Guess Row:")) -1

That means, that if user inputs number 5, it actually means index 6. But with "-1" in the end, it subtracts 1 from user input and therefor user input is inline with the indeces.

I hope this helps!


#8

Use below code.. it works correctly...
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = 'X'

Changed Into below

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


#9

That worked! thanks sathishnarayanan


#10

Hi,
I followed the instruction you gave and passed this part , however I still don't understand what ' Set the list element at guess_row, guess_col to "X".' means and what is board[guess_row][guess_col] = 'X'.
I truly don't understand why should I put [guess_row] and [guess_col] behind board and why should I set it equal with 'x'.
I don't know where did it come from and what this code is used for.
Is there anyone can help me? Thank you very much.