The recommended code seems incorrect


#1

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

So, I notice that while creating this Battleship code there is a clear possibility for a user of this code to pick row 0 (or column 0 for that matter), and that would be acceptable since the code interprets it as the first row (due to list indices starting from 0). Similarly picking row 5 would yield the same result as picking something off the board, which is yet another fundamental flaw with the code.

I have tweaked it a bit so that it satisfies my prerequisites to be an easy to use and intuitive code.
Please have a look and tell me if my code makes sense (I've pasted it below)
And am I the only one who gets annoyed by the fact that the "X" for incorrect column and row replaces WHOLE ROWS instead of replacing one particular element of that row?

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_board(board)

def random_row(board):
    return randint(1, 5)

def random_col(board):
    return randint(1, 5) 

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:
    if guess_row not in range(1, 6) or\
    guess_col not in range(1,6):
        print "Oops, that's not even in the ocean."
    elif board[guess_row - 1][guess_col - 1] == 'X':
        print "You guessed that one already."
    else: 
        print "You missed my battleship!"
        board[guess_row - 1] = "X"
        board[guess_col - 1] = "X"
        print_board(board)

#2

here:

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

this is not right, here you do it right:

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

given the list is multi-dimensional list we 2x times square brackets, first to get access to inner list (our column) and then the position in our column.

you can save yourself some headache by simply subtracting one from the user input:

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

this makes your range easier:

if guess_row not in range(5)

and you only have to subtract once instead of two times


#3

Oh yes accessing items from a multi-dimensional list would certainly work like that. It slipped my mind somehow. But still one small doubt exists, won't the inner list (first list that we access) be row and not column ?

Also, your suggestion makes sense. It's a more concise way to write the code.
And thanks a lot for your reply. It's very helpful to get such prompt support and guidance.


#4

Yea, you are right, the inner list would the row, and the values in the inner lists would be the columns

I got it wrong, it happens :slight_smile:

you're welcome :slight_smile:


#6

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