Where am I supposed to write this elif to check if the coordinate has already been guessed?

Question

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.

5 Likes

2 posts were split to a new topic: What Does This Code Do?

Does an elif statement really need to have the condition in brackets? I just tried it without and it was still fine.

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.

2 Likes

Yeah, brackets are used for making debugging easier. They don’t help with the program, just are there for neatness.

A post was split to a new topic: Im really getting confused about coding!

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!

1 Like

Python syntax for else-ifs is as follows:

if condition1:
  # code
elif condition2:
  # code
elif condition3:
  # code
else:
  # code
2 Likes

it keeps showing an error on line 24. I dont understand what is the mistake. can somebody please help?

I think you just timed out the console by not confirming the syntax from line 24.

Hey there!

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.

I don’t know if i really explianed myself…

1 Like

Consider:

a = 1
if a: print (True)
else: print (False)

Now,

if a: print (True)
else if a < 0: print ('non-natural')
else: print (False)

The second line will raise a syntax error:

SyntaxError: expected ‘:’

So the only thing allow to follow else is a colon; hence, elif.

1 Like

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.

What is the link to the lesson you’re referring to?

also, you cannot have an elif unless there’s an if right before it.
else is the last thing if none of the previous conditions have been met.

Thanks for the response. I know this is an old post.

My mistake. I mentioned step-14 and step-15, but I was referring to the instructions in step-13:
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/not-again

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”.

Thanks again.

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:

https://help.codecademy.com/hc/en-us/requests/new

1 Like