Bad Aim


#1



When I input number within the range the code works and I pass. If I put in 3 and 8 for the range I get the two strings "You missed my battleship!" and Oops, that's not even in the ocean"


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 gues_col == ship_col:
    print "Congratulations! You sank my battleship"
else:
    print "You missed my battleship!"
    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)


#2

What you desribe doesn't match what happens when I run your code:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row:3
Guess Col:8
3
4
Traceback (most recent call last):
  File "/dev/fd/63", line 29, in <module>
    
NameError: name 'gues_col' is not defined

#3

I had an error with spelling there just fixed it and am still getting the same problem


#4

No, still doesn't match what you describe.

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row:8
Guess Col:3
0
0
You missed my battleship!
Oops, that's not even in the ocean.
Traceback (most recent call last):
  File "/dev/fd/63", line 37, in <module>
    
IndexError: list index out of range

#5

Oh, it does print both now. I assumed it wouldn't also crash


#6

I'm kind of stumped here because now the 2nd board disappears and it doesn't work.


#7

2nd board? Isn't there just one?

As for why it printed both of those strings, they are both supposed to be conditional and exclusive to each other, but one of them prints unconditionally.


#8

Yeah you're correct about the one board.

So I edited it to

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)

I get one of the strings now but, i'm still getting the indexError:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 9
2
4
Oops, that's not even in the ocean.
Traceback (most recent call last):
File "python", line 36, in
IndexError: list assignment index out of range

the line it's referring to is the board[guess_row][guess_col] = "X"


#9

You have a check to avoid accessing the board out of bounds, are you doing it incorrectly or accessing the board even when you have determined it's out of bounds?


#10

There we go! I figured it out. Thanks a lot!


#11

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