Generated random row & col are not the same as list element shown as "X"


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/2?curriculum_id=4f89dab3d788890003000096#


I don't have any error in my code. I fulfilled the exercise exactly as said in the instruction but in this game we print out ship_row and ship_col to debug our programming. on the other hand, we specify the list element [guss_row][guess_col] to "X", but when I run my code, the random ship_row and ship_col are not the same as list element that is specified with "X"!



#2

This because Python starts counting from 0 and not 1. :slight_smile:


In order to get exact coords from the user input, subtract one to the user's column and row indicating where to put the X, as well as to ship's column and row.


#3

len() starts counting at one? What you mean is that indexes (list, strings) start counting at zero

no? exactly the opposite, we start counting at one and the list starts at zero, so you should subtract one


#4

I mean in this case, If you input (1,1) the X will be assigned at (0,0). Am I wrong?


#5

no, but this means you have to subtract one, not add one to the user input

If you do this, even your not in range(5) is perfectly validated


#6

Oh sorry, yea you're right. I mixed the two. Corrected my first post.


#7

I still don't get it.
let's take a look at my code:

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

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[guess_row][guess_col] = "X"
    print_board(board)

As you said I subtracted the user input by one, but I still got the same problem.


#8

oh, i think i understand.

ship_row and ship_col are the actually coordinates of the ship

the X on the board marks a missed guess, not the ship coordinates


#9

yes exactly! I can't figure it out!
Actually I think we should replace the [guess_row][guess_col] with [ship_row][ship_col], am I right?


#10

in one of the next few exercises we add a loop, giving the user multiply guesses

By marking the users wrong guesses as X on the board, the user knows what coordinates not to guess again.

the ship_row and ship_col are just for debug purposes, so we know where the ship is actually is, this is useful for us while building the game. The user playing the game never gets to see the ship coordinates till the end after they run out of guesses, and lost (or, if they actually guess the ships coordinates)

No, most certainly not


#11

Marking the wrong guesses with X also allows us to check if the user doesn't enter coordinates they guessed before. This will be the task in the next exercise, then the exercise after that, you will add the loop, hopefully then everything comes together for you


#13