Where am I supposed to write this elif to check if the coordinate has already been guessed?
Answer
Our code to this point (in pseudo code) looks like the code below. We want to insert an elif that checks if their wrong guess was one they’ve already made before.
if guess is correct:
print “Congrats! You sunk my ship.”
else, not correct:
if guess is invalid:
print “Not in the ocean”
# ELIF GOES HERE
else, just missed:
print “Missed!”
update element in board to be “X”
print the board
You cannot have an elif without an if before it, so writing it after an existing else would be invalid, because else must be last. Be especially careful with your indentation as well, as the new elif should be on the same indentation level as the if that checks for the guess being invalid.
jjhiggins97 i don’t think they are even supposed to be in brackets lol. I’m pretty sure you write the condition without brackets, but Python knows its a condition since you put a colon (":") after it.
Hey there, I was wondering whether adding ELIF under the first IF is correct?
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."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
Codecadamy counted it as a correct answer tho but still curious if i’m making some kind of logical error?
Thanks!
I see that there are two opinions in regards to where to but the elif statement, my question is what is the difference in putting the elif statemente under the first if, or putting it in the else but with a new if and else statement.
There should be a better explanation of the purpose of this “elif” statement in this step (14) of the project.
Otherwise, without more information, the logic doesn’t make sense until you read the next step (15).
At this point, the student doesn’t know that there will be more “guesses”. Therefore, the logic for this “elif” is unclear.
The instructions are clear on what to do, but not on the “why” of adding the “elif” at this step.
Step 13 mentions:
“How will we know that a location was previously guessed?”
Reasoning:
At this point the user only has one chance to guess, why would there be a process to determine if the location has been guessed before?
It would be clearer to mention in this step that the user will have more than one guess.
As mentioned in the step (step 14):
“In the next step we’ll look at how to give the user 4 guesses to find the battleship”.
Sounds like a continuity problem/copyediting issue with the lesson. Maybe report that to CS via this form, which is available via the main page, CC Help center>Fix a problem> contact CS: