14 Test Run


#1



14 Test Run


My code is printing out that i guessed that one already no matter what input.
I think i know why its doing it. Just need advice on another way to validate it.


Not to print that out


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)

print ship_row
print ship_col

ship_row = random_row(board)
ship_col = random_col(board)
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)and 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

look:

else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
    if guess_row not in range(5)and 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)

you have this 3 lines:

    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

twice. Think about the logic of the game, in which place do they make more sense? remove the duplicates line of where they make least sense)


#3

In the previous exercise it said to add my else statement again


#4

After your new if: statement, add an else: that contains your existing handler for an incorrect guess. Don't forget to indent the code!


#5

okay, so we have bad aim, which start out with the following code:

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)

then we are told to add an if statement to check if the shot is even in the ocean:

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."
   print "You missed my battleship!"
   board[guess_row][guess_col]='X'
   print_board(board)

then we are told to add the existing handler in an else code block:

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."
   else:
       print "You missed my battleship!"
       board[guess_row][guess_col]='X'
       print_board(board)

so, you can't have the "existing" handler before the condition which checks the shot is in the ocean, since you should moved (not copied) that to the else after checking if the shot is in the ocean


#6

Ahh okay so just delete the old one, thank you!


#7

the idea was that you would existing handler to the else statement, you seemed to have copied it, does it all make sense now?


#8

Yeah should have cut the original instead of copying it


#9

yep, we got to that conclusion.

I am asking if the game logic make sense (do you understand it from a logic point of view)? Why we add the if statement for validation, and move the existing handler to the else statement afterwards.


#10

Yeah i do, just didnt even think to delete the old one.


#11