My code passed in "Not Again!" yet it no longer functions correctly


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/5?curriculum_id=4f89dab3d788890003000096
The code included below passed "Not Again!" but no longer replaces O's with X's, and outputs the phrase "Oops, that's not even in the ocean." for every entry except when I sink the battleship. For some reason it never makes it to the last else statement and I don't understand why.

This problem occurred after adding the elif statement checking for the "X" to see if the user had previously guessed what they had just entered in.


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:
    if guess_row 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 print_board(board)


#2

Hi @laughematician760,

Your main error lies in this part:

You can't do this and python will not interpret it correctly. I made this error myself before, so here's what a codecademy moderator directed me to: https://discuss.codecademy.com/t/faq-how-not-to-compare-multiple-values-using-if-condition/42543. Although it is a different language, the idea is the same: by typing what you did, the first expression regarding guess_col will always be interpreted as True, causing "Oops, that's not even in the ocean." to be displayed regardless of the user input :stuck_out_tongue:

As for why the 'X's are not appearing, it is because of this part:

Firstly, the print_board function should be called within this final else statement, and not outside of it. Why is this so? Because we actually only need the 'X's to appear when a guess has been made that is within the stipulated range and missing the battleship (i.e. only under the case of the final else statement).

Secondly, you do not have to reprint this function after calling it. Let us take another look at the definition of this function:

So you see, within this function itself, there is already a print statement, meaning that the function will already naturally print something to the console when called without having to use the print function again.

Hope this helps :slight_smile:

P.S. Did the code really work during Not Again? I mean, it may have passed the exercise, but the same errors were already present during that exercise using this code, right?


#3

First of all thank you for your detailed response along with the helpful link, I really appreciate it.

In regards to the way the program interprets my line regarding what to do if the row or column that is guessed is not in the range, the issue is that the program interprets my if statement as " "true" or if guess_col not in range(5)" since for some reason non empty strings are interpreted to be true.

In the link you provided the language appears to be different from python, and I was wondering if the tendency of the interpreter to read empty strings as false and non empty strings as true is something that holds true across all major programming languages?

Thanks again


#4

@laughematician760, glad that I was able to help out a little :slight_smile:

Unfortunately, being a beginner programmer myself, I am unable to speak for other programming languages :stuck_out_tongue:

@gaurangtandon, since you wrote the post I shared in the link, could you tell us more? Thanks!


#5

It is indeed correct. You can check that out yourself :wink: This method is called learning by experimenting. All you need is an interpreter (like ideone.com) and write some quick code:

def check_truthiness(val, str):
    if val:
        print(str + " is true")
    else:
        print(str + " is NOT true")
		
check_truthiness(0, "Zero")
check_truthiness(1, "Positive number")
check_truthiness(-11, "Negative number")
check_truthiness([], "Empty list")
check_truthiness([1, 2], "Non-empty list")
check_truthiness("", "Empty string")
check_truthiness("HelloWorld!", "Non-empty string")

The output is:

Zero is NOT true
Positive number is true
Negative number is true
Empty list is NOT true
Non-empty list is true
Empty string is NOT true
Non-empty string is true

run online

Since in the statement if val, the interpreter expects val to be a boolean value, and val is not a boolean value, the interpreter will coerce (forcibly convert) val into a true/false value, resulting in outputs as seen above.

Hope it helps! :slight_smile: