14. Test Run - if, elif, else


#1



I'm just running through 14. Test Run at the moment and trying out different scenarios but when I put lines that aren't within the board range in the raw input, it comes up with:

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

I'm also not 100% sure on how to format the if, elif and else statements as the instructions didn't make it too clear... Looking at some other posts on this module, is it possible to nest an elif statement within the else statement? I was under the impression that it's supposed to be in order of if, elif, else...


Replace this line with your code. 
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
elif board[guess_row][guess_col] == "X":
    print "You guessed that one already."
else:
    print "You missed my battleship!"
    board[guess_col][guess_row] = "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."


#2

this line:

board[guess_col][guess_row] = "X"

you try to access the list with the indexes entered by the user. Which (if they are too high) give an index error. So if the number is not to high:

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

should come before you try to access the list, personally if i where to design the program it is the first thing i would do. Never trust user input


#3

I've changed it to:

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
elif board[guess_row][guess_col] == "X":
    print "You guessed that one already."
else:
    if guess_row not in range(5) or \
    guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    
        print "You missed my battleship!"
        board[guess_col][guess_row] = "X"
        print_board(board)

It still comes up with the same error. Is there a way to put the "You missed my battleship!" statement in a separate else statement to "ooops, that's not even in the ocean.". Or is it possible to put it as a second elif statement?


#4

yea, here:

elif board[guess_row][guess_col] == "X":

you still try to access your list before checking that the coordinates are even valid. You could add another elif to check if the coordinates are even valid after your if statement


#5

Okay, I've now done this and it looks to be working:

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

Just seems weird that the exercise would tell you to put it in the wrong order to begin with.


#6

i know, but i didn't design the exercise. Good you made it working


#7

Just one thing I found, if you re-phrase the condition to:

if guess_row not in range(5) or guess_col not in range(5):
    print "Oops, that's not even in the ocean."

don't need the forward slash as far as I can tell.

Maybe try that.


#8

it is not a forward slash, it is a back slash he is using. You don't need it, but it allows you to continue coding on the next line so it isn't wrong either


#9

my mistake, I'm still a newbie here (can you tell??) Thanks for the tip.


#10

explain what? you did suggestions for improvement, and i disagreed. That is it?


#11

Err... OK, not sure where the confusion lies, but I'm pretty sure I just admitted an error on my part and thanked you for the helpful tip re coding. Not looking for explanations really, just trying to learn like everyone else here. :-). Thanks again.


#12

I guess i understood you wrong, then everything is fine.


#13

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