12. Bad Aim - Halp please


#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:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

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

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

What is my error, I am confused? line 33 ran fine in the previous exercise. Any help is appreciated :smile:


#2

there are -1 after the guess's

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

try taking them out
also try replacing "not in" with "!="


#3

Thanks for the help!

This is the new issue:
Console Error:

Traceback (most recent call last):
  File "python", line 33, in <module>
IndexError: list index out of range

the code on line 33 is starred 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 != range(5) or \
    guess_col != 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)

#4

you have the same "else" statements in too places

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

you might want to delete one, mostly the first one but your choice, just delet one of the "else" statements


#5

My apologies, I should have included the instructions, I will still try your suggestion, but here, check these out:

Instructions
1. Add a new if: statement that is nested under the else.
2. Like the example above, it should check if guess_row is not in range(5) or guess_col is not in range(5).
3. If that is the case, print out "Oops, that's not even in the ocean."
4. After your new if: statement, add an else: that contains your existing handler for an incorrect guess. Don't forget to indent the code!


#6

here my code that worked for me:

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
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)


#7

You have solved my issue, if it isn't too much to ask, can you tell me why your code worked and mine did not so I can share in that knowledge? I always like to know the reason behind things.


#8

like I said I thought you had two of the same else statements


#9

That was literally it? lol, thanks!


#10

no problem @snakeatmaus93


#11

Impossible :pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill::pill: