# 12/19 error after adding code but prior ran fine

#1

Prior to adding the #commented out code it ran fine. Added the code WITHOUT the #comment and it runs indefinitely. I then commented my added code out - reran and received the error below. BTW row 26 is
guess_row = int(raw_input("Guess Row: "))

What am I doing wrong?

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)

print ship_row
print ship_col

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

Gets error 3 times it retries to generate the battleship location
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
3
0
Guess Row: Traceback (most recent call last):
File “python”, line 26, in
ValueError: invalid literal for int() with base 10: ‘RUN’

#2

Have you indented your code correctly?

#3

Yes. I don’t know how to show the code indented correctly in the editor - I am sorry. Again, the 1st time I wrote and ran this code it ran fine:

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)

print ship_row
print ship_col

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

I then added this code without the comments and it ran indefinitely:
#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 I commented it out to make sure I was sure the 1st part ran and it gave me the row 26 error.

#4

I went back to the previous 3 lessons and reset my code - rewrote it the exact same way - I swear! - now it works. weird

#5

How do I format code in my posts?

#6

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