Test Run


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/5?curriculum_id=4f89dab3d788890003000096


So here's my error. Im testing out this thing and for some reason it gives me an error that says "list index out of range" when i use guess_row = 5 and guess_col = 5. however, when i test the game at 6,6, it gives the correct message saying that its not even in the ocean. i tried messing with the indexes and the original list but im stuck. i can move on to the next exercise, but i dont want to until this gets fixed.


from random import randint

board = []

for x in range(1, 6):
    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))

def random_col(board):
    return randint(0, len(board[0]))

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


#2

lists are zero indexed based, so for a 5x5 board the indexes are: 0, 1, 2, 3 and 4

If you want to compensate for the fact that humans start counting at one, subtract one from the user input, manipulating the user input is easier then manipulating the lists


#3

even then, that doesnt fix the problem, it just moves it to index 6,6.
what it should be doing is giving the message "Oops. That's not even in the ocean."
what it is displaying only for coordinate 6,6 (after your suggestion) is the error.
however, this is the only coordinate that gives this error. 7,7 displays the correct message and anything after that displays the correct message.


#4

There is no index 6,6? Both for the list (4,4 is max) and human count (5,5 is max), how did you manage to get 6,6?

Can i see how you applied the fix to compensate for human vs computer difference in count?


#5

i realize theres no index 6,6. which means it should go through the path in the if,then statement that should display the message "Oops, thats not even in the ocean." im confused because when the row guess is 7 and the column guess is 7 (index 7,7) it displays "Oops, thats not even in the ocean." as in its not on the board, the ways its supposed to. the same should apply for row guess 6 and column guess 6 (index 6,6), but instead it gives an error message that says "list index out of range"


#6

i ran the most recent version of your code i have, and this happened:

and 6,6 is caught by the if statement


#7


heres what happens when i run it through the website. could it be something with codecademy?


#8

shouldn't matter, can i see an updated version of your code? Seems you changed something


#9

from random import randint

board = []

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

also, may i know which compiler you are using?


#10

now the user can enter 1-5, which matches the board (0-4)

so you range should range(5) to validate the shot is on the board (for this we use 0-4)


#11

that fixed it! i had messed with all the ranges and indexes earlier and didnt change it back. Thank you! also, Which compiler are you using?


#12

yep, you did. Good you sorted it out :slight_smile:

What does it matter what compiler is used? Python is an interpreted language, not a compiled one

If i need to debug code for people on the forum? Lesson or repl.it
For the rest i used python on my linux install and the computer i have configured i have as server


#13

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