Stuck 13/Not Again!


#1



I am stuck in exercise 13/Not Again! Can you please tell me what did I do wrong? I looked through other postings in this forum & can't figure out what is wrong :frowning: I added the line 'You guessed that one already', but error messages keep telling me I didn't put there in elif statement. Thank you.

Oops, try again. Did you remember to add the "You guessed that one already." message in your elif statement?


from random import randint

board = []

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

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

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print 'Congratulations! You sank my battleship!'
    board[guess_row][guess_col] = 'X'
elif 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':
    print 'You guessed that one already'
else:
    print 'You missed my battleship!'
    board[guess_row][guess_col] = 'X'
    print_board(board)


#2

You're assuming that you've done what the error message says that you have not.

Compare what the error message says you don't have, to what you have. If it's slightly different, then it's different.


#3

Well you seem not to have followed the instructions. It you should be doing this:

   # Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed 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 guess_row == "X" and guess_col == "X":
                print "You guessed that one already."
                
            else:
                print "You missed my battleship!"
                

               
            board[guess_row][guess_col] = "X"
            print_board(board)

Be sure to indent the code properly. After the first else, all other if,elif and else are conditions to the else.


#4

Is board[guess_row][guess_col] == "X": the same as
guess_row == "X" and guess_col == "X" ?

I am quite confused with this segment of the code. I don't understand how (board[guess_row][guess_col] == "X":slight_smile: makes sense.


#5

I also do not understand code above. Although I'm using the below and seems to work...

(make sure to include proper indents of course!)

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
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":
print "You guessed that one already."
else:
print "You missed my battleship!"

    board[guess_row][guess_col] = "X"
print_board(board)

#6

I add the same code but still does not go through and gives the following error. Help Please!!!

Oops, try again. Did you remember to add the "You guessed that one already." message in your elif statement?

from random import randint

board = []

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

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

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    
else:
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
        
    elif guess_row == "X" and guess_row == "X":
        print "You guess that one already."
    else:
        print "You missed my battleship!"
        
    board[guess_row][guess_col] == "X"
    print_board(board)

#7

Here:

You guessed row twice. It should be:

            elif guess_row == "X" and guess_col == "X":
                print "You guessed that one already."

You could do a detailed check on yours as against my post.

Hope this resolves. :smile:


#8

Hey textsurfer59794,
you code is good, but in the print statement below, you have missed the ".(dot)"

elif board[guess_row][guess_col] == 'X':
print 'You guessed that one already'

it should be like

elif board[guess_row][guess_col] == 'X':
print "You guessed that one already."


#9

When you type board[guess_row][guess_col], you're basically accessing the element in that row, and that column. For example, if guess_row = 3 and guess_col = 2, then you're accessing the element in the third row, second column.
Think of it as a grid, and you can access any square in the grid by specifying the row and column.
When you're checking for if board[guess_row][guess_col] == "X", you're checking if the player has already guessed that particular position. An earlier part of your code ensures that whenever the player guesses a position, it changes the element in that position from 'O' to 'X', just to show the player that they've already guessed that position,
But, if they do guess that position again, this piece of code checks if they've already guessed that position. If they have, then it displays the message - "You have already guessed this position"
It basically makes sure that the player doesn't guess the same position twice.


#10

What is the use of adding this code:

elif guess_row == "X" and guess_row == "X":
print "You guess that one already."

The program will only run once, so you will never be able to guess a second time.
Really dont understand the purpose of it.


#11

I'm completely stuck here, tried everything posted on this thread - am I missing something really obvious? Thanks!

My code (where / indicates one indent, // two):

Write your code below!

if guess_row == ship_row:
/print "Congratulations! You sank my battleship!"
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)
elif board[guess_row][guess_col] == "X":
/ print "You guessed that one already."
/ else:
// print "You missed my battleship!"

it keeps saying invalid syntax for the elif line!


#12

Please, I don't think you went through this thread in detail. Your error would be in the elif statement.

It should be:


elif guess_row == "X" and guess_col == "X":

Please refer to : my full suggested code above.


#13

Well I passed with the same elif line

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

and I passed this lesson... it's also working with your version..but I don't know what is the difference


#14

My bad! It should go through really. It should do same. :slight_smile:


#15

This workd,..you need shift some stmts..!!

from random import randint

board = []

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

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

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

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

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
    
    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":
        print "You guessed that one already."
    
    else:
        print "You are out of range"
    
    
    print_board(board)

#16

Thank you! This now works:
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"

else:
print "You missed 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":
      print "You guessed that one already."

 else:
      print "You are out of range"


 print_board(board)