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