# 2 questions about the final code. THX

#1

2 Questions:

Question1. when I input:
1
100 (any number >4)
it does not show:
"Oops, that's not even in the ocean."

but when i input:
100 (any number >4)
1
it shows:
"Oops, that's not even in the ocean."

why? Where is my mistake.

Question 2:

I tried to allow input nothing, and give another chance to re-enter by using the following code:

``````raw_inputa = raw_input("Guess Row:")
col_inputa = raw_input("Guess Col:")

if raw_inputa == "" or col_inputa == "":
turn -= 1``````

in the for loop
Yeah i want it doesn't count turn when input nothing.
but when i input nothing, the for loop still count turn
can't i use: turn -= 1 to cancel this count?
how to achieve my goal in a correct way?

Than you very much!

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

for turn in range(4):
print ("ruound %s" %(turn +1))

raw_inputa = raw_input("Guess Row:")
col_inputa = raw_input("Guess Col:")

if raw_inputa == "" or col_inputa == "":
turn -= 1
else:
guess_row = int(raw_inputa)
guess_col = int(raw_inputa)

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
break
else:
if (guess_col < 0 or guess_col > 4) or (guess_row < 0 or guess_row > 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)
if turn == 3:
print ("Game Over")``````

#2

Well, you have a rather large bug.

Next!

In this case a while loop will do what you need. Also we want to compartmentalize the code so it is easier to rerun.

Python Example:

``````def get_input():
while True:
tmp = 0
try:
tmp = int(raw_input("Enter a number 1-5")) - 1
if 0 < tmp < 5:
return tmp
except ValueError:

There, now we have something that will take out player input and adjust it all the while it only returns a value when it is a valid number between 1-5.

If you do not understand why I used something ask away.

Also to implement it just paste the code, and then change the lines where you ask for player input to call this function instead.

#3