Danger Will Robinson


#1

Hi Everyone

Can someone please help me understand this error message: You're updating 'guess_row - 1' and 'guess_col - 1' instead of 'guess_row' and 'guess_col'.

Here the code:

from random import randint

board = []

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

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

print_board(board)

def random_row(board):
    return randint(0, len(board[0])-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

Thanks

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



#2

Side Note: you don't need to access the 0th index for your row,


but your actual problem is that you don't have the if/else block in your code. :slight_smile: Go back one exercise follow the instructions carefully and then carefully read the instructions for Danger Will Robinson as well.


#3

Hi bandit

I've append this code:

if guess_row==ship_row and guess_col==ship_col:
print "Congratulations! You sank my battleship"
else:
print "You missed my battleship!"
print_board(board)

The error still remains...is this the correct randint syntax...

def random_row(board):
return randint(1, len(board[1]))


#4

Your previous syntax was correct you just had to remove the [0],

def random_row(board):
    return randint(0, len(board) - 1)

is your if/else indented properly like,

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)

#5

Thanks bandit the code is working! The lack of indentation made no difference in IronPython. I had to "cheat" to get passed the error (print out the ship's location).


#6

That wasn't necessary cheating :sweat_smile: but glad it worked out.


#7

Hi bandit..I've completed the exercise bandit...no cheating :smile: but the limbo elif condition doesn't seem to hold true...a violation causes the code to crash.

elif (guessrow < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):_
_ print "Oops, that's not even in the ocean."**


#8

elif (guessrow < 0 or guess_col < 0 ) are valid conditions. -1, -1 returns "Opps...."

However guess_row > 4 or guess_row > 4 returns the following...

"Traceback (most recent call last): File "python", line 43, in IndexError: list index out of range"


#9

please post all your updated ode


#10

Hi bandit...

Here's the full code:

from random import randint

board = []

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

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

print "Let's play Battleship!"
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)

for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break 
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        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!"
        if turn == 3:
                 print "Game Over!"
    board[guess_row][guess_col] = "X"
print (turn + 1 )
        
print_board(board)

#11

the problem is in your final else block,


1) It would be better to put this line, print turn + 1 right after your for loop creation,

for turn in range(4):
    print(turn + 1)

2) Make sure your turn check if turn == 3 is indented properly,

this,

should be,

if turn == 3:
     print "Game Over."

3) board[guess_row][guess_col] = "X" should be inside yourelse: if condition: statement,

else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"

and so shouldprint_board(board).


By making all the necessary changes your code should work fine. :slight_smile:


#12

Thanks bandit...your are really Monty python!


#13

No problem man! :smile:


#14