14/19 - debugging, found bugs


#1
  1. When I guessed 4 and 2 (at a time when the ship was not located at those coordinates), I got:

Oops, that’s not even in the ocean.
You missed my battleship!

That guess is in the ocean.

  1. When I guess coordinates that actually are outside the board, like 9/9 or 6/1, it doesn’t tell me “Oops, that’s not even in the ocean.” it just gives me an error:
Traceback (most recent call last):
  File "python", line 31, in <module>
IndexError: list index out of range

#2

Can you post your code please? :slight_smile:


#3

Since posting this, I fiddled with it a little bit and it is giving me new problems. Now, it stops me after I enter the first response. If the first guess is “in the ocean,” it says “You missed my battleship!” and if it’s not in the ocean, it gives me that “traceback” error. It then prompts me for a second coordinate, but it just freezes up after I press enter.

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)
print ship_row
print ship_col

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

if guess_row == ship_row and guess_col == ship_col:
  print "Congratulations! You sank my battleship!"
# The "if" statement works.
elif board[guess_row][guess_col] == "X":
  print "You guessed that one already."
# I don't know if the "elif" statement works.
else: # The "else" statement is broken.
  if guess_row not in range(0, len(board[0]) - 1)  \
  or guess_col not in range(0, len(board[0]) - 1):
    print "Oops, that's not even in the ocean."
  print "You missed my battleship!"
  board[guess_row][guess_col] = "X"

#4

this code:

elif board[guess_row][guess_col] == "X":
  print "You guessed that one already."

is before you check the shot is actually in the ocean, as such, index error can still occur.

The structure suggest by exercise:

if hit:
   print "you won"
else:
   # handle different scenarios for missing \
   # in this else clause
   # first check if in ocean
   # else if check if already guessed
   # else handle missed shot

following this structure, a index error should be impossible


#6

So you’re saying I should not be using elif statments between if and else? Just an if statement and an else statement, which itself is followed by a series of if or elif statements?


#7

yes, exactly. if (hit) and else (miss), then in the else clause, handle the different scenarios for a miss


#8

Ohhhh, thank you! I’ll try that.


#9

inside the else clause, the order you handle the different scenarios for a miss matters


#10

Thank you! It got a lot worse before it got better; but I kept coming back here and typing out the problems I was having and my thoughts about it, which kept leading me to new insights. So then instead of submitting my reply I’d go back and change something. Then the problem would evolve and I’d come back to this window and type out what was going wrong, and the cycle would repeat. Eventually I got to the bottom of it and my game seems to be working.

The only thing I’m running into that I don’t understand is why it prints the word “None” after my grid. Not too worried about it, though.


#11

do more you do on your own, the better it is actual. The less we help, the better. Trying to just give a nudge in the right direction is best. Running into new problems and gaining new insights is one of the best learning experiences you can have

you could run outside of codecademy, see if codecademy’s exercise validation is the problem (repl.it for example), otherwise post your code and i will have a look


#12

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