I can't sink the battleship


#1



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


what's the meaning in list element and how do i change it into "X"?


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


#2

this line:

[guess_row][guess_col] = "X"

guess_row and guess_col are the coordinates you need to access your multi dimensional array which holds your board:

board[index1][index2] = "X"

#3

even when i change it to:
board[ship_row][ship_col]='X' it is working but there's still an error:
Oops, try again. It looks like your game does not change the O to a capital X for an incorrect guess.


#4

can you post an updated version of your code?


#5

these are the last 2 lines:
board[ship_col][ship_row]='X'
print_board(board)


#6

and by the way, do you remember how to flat a number down to his integer form?


#7

wouldn't it make more sense to the row first? given how your multi-dimensional array is build? Can i see your full code?

yea, you can use int(), this will floor the number but you won't need it in this exercises


#8

i know i won't I'm working on multiple exercises


#9

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)

it tells me: Oops, try again. It looks like your game does not change the O to a capital X for an incorrect guess.


#10

maybe i need to change it that the player can type from 1-5 instead of 0-4 but how do i do this?


#11

no, the problem is here:

board[ship_row][ship_col]='X'

you mark the ships locations with X, while you should marked the guessed location:

board[guess_row][guess_col]='X'

For the exercise it is not recommended to let the user enter 1-5, but if you want you can do this simply by subtracting one from the user input:

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
guess_row -= 1
guess_col -= 1

but then you would have to validate that the user doesn't input 0, it is more difficult and most certainly not recommended for the exercise


#12

thanks that really helped