14. Test Run - Code works, but not as expected


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/4?curriculum_id=4f89dab3d788890003000096#


My code works and allows me to move on to next lesson. However, I'm not ready to move on because it isn't implementing the string message "You guessed that one already." when that condition is met. After reviewing my code I'm pretty sure I know why this is the case. It's because the elif is running before the else below it, which is the condition that sets the location to 'X', isn't it? So it's asking else if the guess location is X before it knows that it will become X.


If I'm correct about this, what is an efficient way to fix it? Or should I leave it as is and move on where it will eventually be fixed in a later lesson?


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!'
    
else:
    
    ship_row = random_row(board) #I've copied/pasted this code from above
    ship_col = random_col(board) #for testing purposes, otherwise the elif
    guess_row = int(raw_input("Guess Row:")) #I've added in this lesson
    guess_col = int(raw_input("Guess Col:")) #would never be met.
      
    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)


#2

If you simply want to print You guessed that one already - to check if your ELIF works, change it to:
-elif board[guess_row][guess_col] == board[guess_row][guess_col]:

That's how I checked this.


#3

Well I was looking for a solution that lets the code work as intended rather than just test if it prints. Unless this will be addressed in a later lesson.


#4

Above, you're checking if board[guess_row][guess_col] equal to 'X', not your guess.

Whatever number that's typed in guess_row/guess_colis officially stored in here. So if you didn't use the comparison operator to compare it with'X'in yourelifstatement, yourelif will run if the same number is repeated. No comparison needed.


#5

Hey thanks for that insight! However I think it will help if I include the instructions from the lesson:

  1. Add an elif to see if the guessed location already has an 'X' in it.
  2. If it has, print "You guessed that one already."

Your suggestion would work, but it would only check for the most recent guess. The way it should be is check for all previous wrong guesses.

The way I'm understanding is that it wants me to check if it's X because X is what is assigned when a previous guess was wrong, which is checked after this elif. This is why I think it's not working, but it's how the instructions ask me to set it up.


#6

I've updated the title to the next lesson as I've moved on to it and it's instructions literally are to thoroughly test code to make sure it's functioning as expected.

Currently code still won't print 'You guessed that one already' and I believe it's because of the flow of the script since it's checking for X before X is even assigned.


#8

@stetim94 Could you shed some light here? I want to make sure everything is working properly before moving on. Since my last post about my thinking of why it's not working I've tried moving around the flow of the different checks but am still not able to get it to print. Here's one example of what I tried:

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!'
    
else:

    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    
    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.' 
    
    elif guess_row in range(5) and guess_col in range(5):
        print 'You missed my battleship!'
        board[guess_row][guess_col] = 'X'
        print_board(board)
            
    """else:
        print 'You missed my battleship!'
        board[guess_row][guess_col] = 'X'
        print_board(board)"""

#9

here:

    elif guess_row in range(5) and guess_col in range(5):
        print 'You missed my battleship!'
        board[guess_row][guess_col] = 'X'
        print_board(board)
            
    """else:
        print 'You missed my battleship!'
        board[guess_row][guess_col] = 'X'
        print_board(board)"""

why did you comment out the else? I would just use else, given its the only scenario remaining, elif guess_row in range(5) and guess_col in range(5): is a unnecessary check given it has to be in range, otherwise not in range would have ran

when the user doesn't guess the ship coordinates, don't give them a new guess. just handle the incorrect guess, you will add a loop in one of the next exercises, then the user get multiply guesses

so remove the second set of prompts:

if guess_row == ship_row and guess_col == ship_col:
    print 'Congratulations! You sank my battleship!'
    
else:
    # remove this two prompts below
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

see comment, then you should be all good


#10

Thanks for the response. I ended up doing multiple variations and this was just the most recent one. I also did try using else: and got the same result. The only reason I have the second guess is for testing (was going to delete after) to see if "You've guessed that one already" will print when I guess the same thing twice. It doesn't which is what I was trying to fix before moving on. However, as you said I will add ability for multiple guesses in a later lesson. Plus, since the code does run successfully I moved on to the next one to see and the code changes on it's own anyway. That being said, there's no point in trying to fix since code changes in next lesson, but once I add multiple guesses if I still can't get that line to print I'll open back up. Thanks again everyone!


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.