Bad Aim, yet another


#1

Hello,

I have not been able to create a solution to my code based on other's dilema. I get the following error for the code that follows the error.

Error:
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: 4
Guess Col: 5
0
3
You missed my battleship!
Traceback (most recent call last):
File "python", line 33, in
IndexError: list assignment index out of range

My Code:

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

Thanks to all that are helping with this section!


#2

you updated your board with X before you check the hit is actually one the board


#3

Oh my, that was an easy fix and one I would not have seen. @stetim94 I owe you a beverage for your efforts. A big thanks to you!


#4

@stetim94,
I put the following code in Python 3 and it runs without an error. After I input a guess row and column, it reprints the board and does not put an "X" on a guess in range. How do I bridge the gap to real python?

from random import randint
board = []
for i in range(0, 5):
    board.append(["O"] * 5)

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

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

ship_row = random_row(board)
ship_col = random_col(board)


guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Col: "))

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

#5

// handles correct guess
if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sank my battleship!")
// handles incorrect guesses
else:
      // different cases for wrong guesses
      // if not on board
      // else

#6

Ok, I got the board to print once and have an "X", but "you missed" prints twice. Does it matter which line of code I remove and how do I determine which?


#7

i would remove the first one, given you missed battle ship and not even in the ocean is a bit double


#8

I've got the code working in both CC and Python without duplicate print statements. I removed the 2nd "you missed" because it seemed to be out of place. Maybe I got lucky with that one.


#9

I spoke too soon. Python is working, but CC is giving an error. My guesses were 9 and 1

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

My code:

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

#10

you should really use the structure i gave you here:

// handles correct guess
if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sank my battleship!")
// handles incorrect guesses
else:
      // different cases for wrong guesses
      // if not on board
      // else

the comment also explain a thing or two


#11

I am confused. What am I missing? I think my code looks the same as yours.


#12

there is a reason i indented the comments? You still update the board:

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

before you check the shot is actual on the board:

if guess_row not in range(5) or guess_col not in range(5):

#13

I'm not making the connection. I indented for the, but yours are on the same line. Is my code incorrect or is it format?


#14

My code works, except for a guess that is not in range. I am not able to figure out how to correct my code.


#15

After checking for a win, the next check should logically be the range. If you are inputting numbers greater than 4, the check should anounce, "Not even in the ocean!".


#16

When I enter 9, 2, I get the following error:
Traceback (most recent call last):
File "python", line 36, in
IndexError: list index out of range

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
if guess_row > range(5) or guess_col > range(5):
print "Oops, that\'s not even in the ocean."
else:
print "You missed my battleship"
board[guess_row][guess_col] = "X" . #this is line 36
print_board(board)


#17

This is an invalid comparison. A number cannot be compared to a range (a type of list). Check to see if the number is IN the range, or NOT.


#18

I watched this YouTube Video, https://www.youtube.com/watch?v=7Ki_2gr0rsE&t=892s, and that is how the user created his, but didn't test the outer possibilities. I will make the change.........and that worked!!!

That would seem like and easy short-hand.


#19

I put the following code in Python 3 and got the following error, trying to do the same section as CC.

Error:", line 29, in
board[guess_row][guess_col] = ("X")
IndexError: list index out of range

My Code:
from random import randint
board = []
for i in range(0, 5):
board.append(["O"] * 5)

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

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

ship_row = random_row(board)
ship_col = random_col(board)

guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Col: "))

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")
if guess_row not in range(5) or guess_col not in range(5):
print ("Oops, that\'s not even in the ocean.")
print_board(board)


#20

Still not in the correct order. It should follow immediately after the check for a win.