12/19 Bad Aim [SOLVED]


#1

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 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)

What am I even doing wrong?


#2

Here's my code that's "passing", even though something is seriously wrong with it, I'm not sure what.

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:
    "Congratulations! You sank my battleship"
else:
    if board[int(guess_row)] not in range(5) or \
        board[int(guess_cow)] not in range(5):
            print "Oops, that's not even in the ocean."
    else:
        print "You missed my battleship!"
        board[int(guess_row)][int(guess_col)] = 'X'
        print_board(board)

#3

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 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)

It is your second to last line needs to be corrected. You don't need to initialize it to an int because it is already an int by storing a number. Otherwise, you have the program :wink:


#4

This code generates the correct error messaging, but the lesson does not pass:

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

The lesson does pass when out of range inputs generate a Python error...


#5

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(0,ship_row-1) or guess_col not in range(0,ship_col-1):
print "Oops that's not even in the ocean"
else:
print "You missed my battleship!"
board[guess_row][guess_col]="X"
print_board(board)

This code keeps giving a run time error to make sure that "oops that's not in the ocean" needs to be printed,even thought it is getting printed on the terminal


#7

Within the if statement of your first else, you wrote "board[int(guess_cow" instead of guess_col


#8

I've just been struggling through this one and was partly relieved / disappointed in myself haha...

print "Oops that's not even in the ocean" is what you have written,

print "Oops, that's not even in the ocean." is what you need to write, EXACTLY as shown,


#9

I make the exact same mistake arrggh


#11

Your code is checking to see if the guess is in range of 0 and the location of the ship. That will pass the assignment, but isn't correct. It should check to see if it's on the board. The following code works - it passes the assignment and yields correct results.

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    if guess_row not in range(0,len(board)- 1) or \
        guess_col not in range(0,len(board) - 1):
        print "Oops, that's not even in the ocean."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)

#12

Hey byteace39652,

When I tested the code above I found it didn't work right for the corner cases (row or column = 4). The following does work for that and fails if you try using row or column = 5.

elif guess_row not in range(0,len(board)) or \
    guess_col not in range(0,len(board[0])):
        print "Oops, that's not even in the ocean."

The other benefit with this code is that the number of columns is accounted for. Assuming you've got a fixed length for all nested lists, this will account for board width that isn't the same as board length. The other code wouldn't properly account for a board that is 6x10. It would validate assuming the board is 10x10, as the length of 'board' is the number of rows.