12/19 Test Run -- Any opinion on the "Error & How I Fix It"


#1

My problem code is the one that determine the user input whether it is out of range or not. When I talk about the code later here, I refer to this one.

This one is my error message.

I tried to fix it by changing the order within the first “else”, but the error was still there so I move the codes lines to “else if” above the first “else”. Here is the picture of it. The program work as I expected.

Here is what I wondering:

The instruction told me to put that code under the first ‘else’ as shown in both pictures, but the first time I did that, the program gave me an error message so I fixed it as shown in the second picture. My question is “Will this come back to bite me later? Or this can be an alternate way of writing this code?” At this point I still can’t think of any future problem, but if there will be one that I should foreseen, I would like you to enlighten me. Especially if it would become a serious problem later. I don’t want to develop a bad habit without knowing it.

Thank you.


#2

i went over the instructions many times, they are a bit tedious, but if followed correctly you end up with:

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess 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(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)

if you would describe this in english we get:

if hit:
  print you win
else:
  this else clause handles all the misses

within the else clause, you make additional conditions for the different scenarios of missing.

which a sensible structure.


#3

I agree. That is what I thought but at that time, it didn’t work. I’ll check back later. Today I have just got the ‘SCT’ error message on firefox. I’ll see what I can do in Chrome.


#4

if done right, it should absolutely work


#5

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