15/19 Stuck


#1

for turn in range(5):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col:
  print "Congratulations! You sunk my battleship!"
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
      print "Oops, that's not even in the ocean."
elif (board[guess_row][guess_col] == "X"):
  print "You guessed that one already."
else:
  print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
# Print (turn + 1) here!
print "Turn", turn + 1
print_board(board)

It keeps telling me that something is wrong with line 37, which is the first elif statement in the loop.


#2

It seems maybe the indentation is a bit off? This is the correct code (which is the same as yours) but I'm thinking maybe the indentation might be off.

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

Set the indentation of the "elif" statement equal to the "if" statement above it.


#3

****Please help!** I continuously get the error: Did you remember to print out+1each turn?**
Thanks again!

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

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
for turn in range(4):
print "Game Over"
print "Turn",turn+1
print_board(board)


#4

Similar problem myself, Figured out a solution yet?


#5

I get also the same error "Oops, try again. Did you remember to print out turn + 1 each turn?"
I did try different positions in the for loop and different versions to give it out, but it never does get it. Even then it's visible on the print out e.g.:
O O O O O
O X O O O
O O X O O
O O O X O
O O O O O
Turn 4

Would really like to know how to complete the course.


#6

I had to copy the "print "Turn", turn + 1" verbatim to get it to work even though the syntax I had been using worked flawlessly.


#7

can you put that in code format PLZ?


#8

this code is work for me..alhamdulillah..hope can help..

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    print "Turn",  turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"        
            turn = turn + 1
            print turn
    print_board(board)

so the poin is that when we put > for turn in range (line 28), then we should indent the code below..


#9

thx that helped me XD ;D


#10

This seems to do the trick. I've been trying to stick to Python 3 syntax, but something about the print command seems borked here. print() will return the parentheses as part of the actual print statement, which messes something up in their automatic grader, it seems.