To Your Battle Stations! Testing and debugging help needed


#1

Hey guys, i found some errors, that I don't know how to solve, for example if dont give any raw input(don't type anything) the program would give me this command:-
Traceback (most recent call last):
File "python", line 36, in
ValueError: invalid literal for int() with base 10: ''

I tried everything, but can't find a way. I'll post some other errors that i might find here too.. so this might be a long post though.. sorry but for now..

Here is the code for that^ error stated above:-

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)

Everything from here on should go in your for loop!

Be sure to indent four spaces!

def turn_count():
if turn == 3:
print "Game Over"
print "The Real Location of the ship was:-"
print "row= " + str(ship_row)
print "col= " + str(ship_col)
else:
print "Try Again"
for turn in range(0,4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if len(str(guess_row)) == 0 or len(str(guess_col)) == 0:
print "you didn't enter the location correctly"
elif guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
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!"
board[guess_row][guess_col] = "X"
# Print (turn + 1) here!
print_board(board)
print (turn+1)
turn_count()

Thanks guys :blush:


#2

And Also, that even when the person has successfully sunk the ship, the loop keeps going on, any ideas how to get out of the loop?


#3

You can break when they hit the ship.


#4

okay, that did it. thanks.. any ideas on the problem there?


#5

You could do a check for if the len(input) > 0.


#6

i didn't get that, maybe write that line for me please?


#8

After

for turn in range():

On line 35 put something like:

while (len(guess_row) == 0 or len(guess_col) == 0):

Then put the inputs for row and column in there. (And an error message)

Then remove the if-statement on line 38, as this takes care of it.


#9

No, this wouldn't add any functionality.


#13

This my code with a little extra, it prints out the letter S where the ship is:

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)
print ship_row
print ship_col

for turn in range(4):
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!"
    board[ship_row][ship_col] = 'S'
    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!"
        board[guess_row][guess_col] = "X"
    print "Turn", turn + 1
if turn == 3:
    print "Game Over"
    board[ship_row][ship_col] = 'S'
print_board(board)