#12 Bad Aim


#1



I do not see my error, please will someone help me? Thank you


Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: list assignment index out of range


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


#2

you try to updated your board:

board[guess_row][guess_col] = 'X'

before check if you shot is even in the ocean:

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

i would update the board here:

else:
    print "You missed my battleship!"
    # mark ship as missed

#3

These three lines should be the last lines of your code, insdide of your second else statement, meaning you want it to look like this:

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    if guess_row not in range(5) or guess_col not in range(5):
           print "Oops, that's not even in the ocean."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)

You have to check if guess_row and guess_col are in range BEFORE you print that they missed.


#4

Thank you for making the explanation clear.
To improve I have been going back to early sections
just to see if I can find the mistakes, before. reading mods
You are alwas specific on your feedback, thank you


#5

No problem, glad I could help.

That's always a really good idea going back to previous lessons can honestly help to prevent errors.


#6

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