Bad Aim (Can't figure out fix)


#1

I can't figure out what's wrong because the error is for line 33, which was not new code, it was from the step before


#2

As with the line 33 error, you have an IndexError, which means that the indexing is out of range. You should do:

board[guess_row][guess_col] = 'X'

Watch out for other tab spaces in other lines. :wink:


#3

I made your suggested edits, but I'm still getting the same error :frowning:


#4

Hi, line 35 should be.

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

#5

Thanks, but I'm still not able to fix my issue with the error in line 33


#6

Okay, may I see all your code? Copy and paste here. Please, use the < / > to format code.

Thank you!


#7

from random import randint</p>
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

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)

#8

Remove the p tag here.

Also, you aren't meant to indent this:

It should come directly below.

Let me put it all together.

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


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)

Let me know if this resolves.


#9

I feel like I'm insane, but I still keep getting the "Traceback(most recent call last): File "python" line 33 in index Error: list index out of range, but I swear I have exactly what you have:

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

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)

#11

ok...i found whats wrong with your code. Remove the first print block after first else statement.

The code is going to run that first and its always going to produce and error. So if you remove that and put the "If" statement checking the range first, there will be no error.


#12

See comments above this and see if it resolves.

Remove these lines in the first else block and see:


#13

That was it! Thanks for the explanation, and thanks to you both for helping out!!


#14

Hi, I have one question, maybe u could help me...
i am rather new but,

shouldnt the correct line of code be:

board[guess_row - 1][guess_col - 1] = 'X'

because the line

board[guess_row ][guess_col ] = 'X'

places "X" at the board row/column index position
and not at the user inputted row and column position
(he/she shouldn t care that first row/col are at index 0)


#16

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