Danger,Will Robinson


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

Ok, so this is my code and that is the output:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 3
2
3
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O X O
O O O O O
None

i've passed the test, but originaly i wrote:

else:
    print "You missed my battleship!"
    board[(guess_row)-1][(guess_col)-1] = "X"
    print_board(board)

and the output was:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 3
3
0
You missed my battleship!
O O O O O
O O O O O
O O X O O
O O O O O
O O O O O
None

But then i didn't pass the test and the console threw me an error saying that i dont need the "-1" there.
From my point of view, what i originaly wrote was correct, and you can see on the output (line 3, col 3).

Can some one explain why? i'm realy confused...

Sorry if i wrote something wrong, english isn't my native language.
Thanks :smiley:


#2

And i know that the index starts at 0, but the man who will pay wont know that, he just want to hit the 3º line with the 3º colum


#3

i would simply subtract one from the user input:

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

this way, you make the one based counting zero based counting to make it compatible with your lists, making the program more user friendly, but manipulating the user input which is a lot easier the missing with the indexes of your lists


#4

That is very clever! i feel embarrassed haha.
Thanks :slight_smile:


#5

Hi, @blogblaster96559 ,

EDIT (December 9, 2016): As I was writing the following, @stetim94 was also providing some good advice. Have fun upgrading the game.

Codecademy evaluates your code based on the instructions given in each exercise. You might have some good ideas for improving the design of the exercises, such as presenting the user with the perspective that the rows and columns are numbered from 1 to 5 instead of from 0 to 4. However, if your code is not consistent with the instructions, Codecademy might not pass your submission.

If you wish to improve the design of the Battleship! game, you can copy your code to a location where you can experiment with it in a Python interpreter offline. If you do renumber the rows and columns, remember that you may need to adjust the code in several other locations to conform to the revised system.

You have this if block header ...

if guess_row == ship_row and guess_col == ship_col:

If you use the following code to place the X in the board list...

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

... you may also need to adjust the condition in the if block header that evaluates the user's guess, as well as make other changes.

Also keep in mind that Codecademy still uses Python 2. If you port the game to Python 3, be aware of the differences between versions of Python. See Porting Python 2 Code to Python 3.


#6

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