[Request] Break or simplify my Battleship! code

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
I modified the requested code a bit during the battleship exercise in an attempt to learn more about python. Could someone take a look at this and either try breaking it, or finding ways to simplify the code?

Thanks

<What do you expect to happen instead?>

```python

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)

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

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

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

for turn in range(6):
clear = “\n” * 100
print clear
if turn == 0:
print “Let’s play Battleship!”
print_board(board)
print “Turn”, turn + 1
guess_row = " "
guess_col = " "
while (guess_row == " ") or (guess_col == " "):
try:
guess_row = int(raw_input(“Guess Row:”))
except ValueError:
guess_row = " "
try:
guess_col = int(raw_input(“Guess Col:”))
except ValueError:
guess_col = " "
if (guess_row == " ") or (guess_col == " "):
print “Input error. Try integer.”
elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
guess_row = " "
guess_col = " "
print “Oops, that’s not even in the ocean.”
elif(board[guess_row][guess_col] == “X”):
guess_row = " "
guess_col = " "
print “You guessed that one already.”
else:
break
if guess_row == ship_row and guess_col == ship_col:
print clear
print “Congratulations! You sunk my battleship!”
break
else:
board[guess_row][guess_col] = “X”
print_board(board)
print “You missed my battleship!”
if turn == 5:
print clear
print “Game Over”

<do not remove the three backticks above>

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