Test Run


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

This the code that i wrote. I need help with the return part. When I type any number out of the grid I dont get the proper return message!


#2

if i enter row 3 and column 2 the board gets updated to:

O O O O O
O O O O O
X
X
O O O O O

while it should be:

O O O O O
O O O O O
O O O O O
O O X O O
O O O O O

you just overwrote to whole rows with x instead of updating the users guess with X

maybe revisit 11. danger, will robinson? In the information section above the instructions its shown how to access a specific coordinate

Finally, your game flow isn't perfect. If invalid coordinates (for example 6) are entered, the program will crash because of an index error. the code which should prevent this:

if guess_row not in range(5) or  guess_col not in range(5):

is after (it should be before) you update your board


#3

This is my new code, I got the part with X, Now when I type the answer that is out of the grid I get the proper response but i am still getting the error, is it normal????????

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

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


#4

please post your code with format:

So indent shows


#5

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

#6

This not the same as instructed by the exercise, which follows the following design:

if hit:
    print "win"
else:
    # this else clause handles the different miss cases
    if not in ocean: 
         print "not in ocean"
    elif already guessed:
         print "guessed already"
    else:
         print "missed"
         # update and print board here

this is the programs design in pseudo code.

the problem with your code is that updating the board can still be reached even when not in ocean evaluates to true

if you want to compensate for the fact that lists are zero indexed based and humans start counting at 1, subtract one when prompting the user for input, what you currently did can still give a index error (when 0 is entered) and we can't reach row 5 and column 5 on your board.


#7

Ohk. I got it. Thanks a lot for the explanation!:grin:


#8