13/19 - curious to understand this alternative


I was stuck earlier but found one answer on this bulletin, yet don't understand why this works:
"elif (guess_row and guess_col =="X"):"

What do the parenthesis do here? While "board[]" is needed in the alternative:
"board[guess_row][guess_col] == "X":"

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"

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

elif board[guess_row][guess_col] == "X":
    # elf (guess_row and guess_col =="X"): works, why?
    print "You guessed that one already."    

    print "You missed my battleship!"
    board[guess_row][guess_col]  = "X"
    print print_board(board)


Hi @yellowcurry,

Exercise 13/19 is a bit misleading as it asks that you create a condition that will not be used until 15/19, that is when you have more than one turn implemented.

From what I understand, when you hit "Save & Submit Code", the code validator for this exercise will look for a line with elif and a print statement with "You guessed that one already." but will not specifically evalutes if your elif condition is valid.

For example, using the following elif and submitting your code will have you pass the exercise even if the condition makes no sense:

elif board == "blue":
        print "You guessed that one already."

That being said, the correct expression is indeed elif board[guess_row][guess_col] == "X":

As for your question, the condition elif (guess_row and guess_col =="X"): is invalid for more than one reason, one being that guess_col will never be equal to X since we use int() on raw_input():

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

So inputing anything else than an integer should throw an error message.

Also, the parenthesis do not seem to play any specific role in the expression as there are no operator precedence or anything else warranting their use and they could probably be omitted with no difference at all in the result.

Now, on a side note, click here for a very useful trick you can use whenever you want to paste code into the forum :smile:

Have fun!


savior! thanks! i copied the one with parenthesis from the forum and thought that is a working code! thanks for pointing that out for me!


I didn't pass at first because I forgot the period at the end of the output... :unamused:


I just found that too after about 20 min of debugging. :rage: