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:")) 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 = "X" guess_col = "X" print_board(board) if guess_row not in range (5) or \ guess_col not in range(5): print "Oops, thats not even in the ocean."
Please post the link of the exercise next time.
So what I would change is add an
if-else statement under the
else statement. First is to move the
if statement under the
else, and write the following code under the
else statement of the
print "You missed my battleship!" guess_row = "X" guess_col = "X" print_board(board)
And your code doesn't change the Os to Xs at all.
guess_col do not refer to the exact position on the board, you defined them as numbers here:
guess_row = int(raw_input("Guess Row:")) guess_col = int(raw_input("Guess Col:"))
So in order to change the Os to Xs, you need to use another way to get the exact position on the board.
In this way, it's as similar to accessing an object in a list.
food = ["cake", "pie", "donuts"]
In order to access
"pie", we would write
Similarly, to access the coordinate, we would write
guess_col are numbers.
All that you need to do is assign
"X" to that coordinate.
Say we want to change
"strudel". What we would do is this
food = "strudel".
can you tell me what is wrong here?
That code is in the wrong place. It should be above all the checks except Win.
before even adding that code... i am getting error, i think there is some problem with
guess_row = "X"
guess_col = "X"
As you have explained.... i did some changes,
please check this
hey... this worked for me...
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.