# Further dev of the game 19/19

#1

Hi . I am trying to improve the code by telling the player that he didn’t wrote numbers as choices for the row/col using the while/try/except but it keep backing me to the first turn even when the player wins.I need to mention that Battleships is the last lesson I took and I found the alpha verificaion method on google without working the upcoming lessons. It is going to be explained in the future how the while function works? Sorry for the long post.
Here is the code that i try to run

``````from random import randint
choice=raw_input("Welcome to Battleships!. Write 'yes,'Yes' or 'y' to start and press 'Enter'.")
if choice in  ["yes","Yes","y"]:

board = []

for x in range(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
print "Pick coordonates.(The coordonates are between 0 and 4 and you have only 5 tries.)"

while True:

try:
for turn in range(5):
print "Turn", turn + 1
guess_row = int(raw_input("Pick row and press Enter:  "))
guess_col = int(raw_input("Pick col and press Enter:  "))

if guess_row == ship_row and guess_col == ship_col:
print "You destroyed the ship and won the game!"
break

else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Your coordonates aren\'t on the board"
print_board(board)
elif(board[guess_row][guess_col] == "X"):
print "You have typed this coordonates before."
print_board(board)
else:
print "Try again"
board[guess_row][guess_col] = "X"
print_board(board)
if turn == 4:
print "You lost"
except ValueError:
print "You wrote the coordonates wrong."

else:
print "Exiting game"
``````

And here is the original code: (It runs well if i give the good parameters. Any advice? )

``````from random import randint
choice=raw_input("Welcome to Battleships!. Write Yes and press 'Enter'.")
if choice == "Yes" or choice == "yes":

board = []

# this is creating the board with the use of a list 5/5
for x in range(5):
board.append(["O"] * 5)

#this is deleting the '' and ,  between the elements of the board
def print_board(board):
for row in board:
print " ".join(row)

#this prints the game board on screen
print_board(board)

#this creates an index_row for the ship
def random_row(board):
return randint(0, len(board) - 1)

#this creates an index_col for the ship
def random_col(board):
return randint(0, len(board[0]) - 1)

#this stores the index_row/col to 2 variables
ship_row = random_row(board)
ship_col = random_col(board)

#this is used for debugging(shows the winning indexes)
print ship_row
print ship_col
print 'Pick coordonates.(The coordonates are between 0 and 4 and you have only 4 tries.)'

#this is used to create 4 choices
for turn in range(4):
print "Turn", turn + 1

#this is used to input the player choices

#this is used for knowing if hte player won

guess_row = int(raw_input("Pick row and press Enter:  "))
guess_col = int(raw_input("Pick col and press Enter:  "))

if guess_row == ship_row and guess_col == ship_col:
print "You destroyed the ship and won the game!"
break

#this is used to change the board if the played didn't won
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Your coordonates aren\'t on the board"
elif(board[guess_row][guess_col] == "X"):
print "You have typed this coordonates before."
else:
print  "Try again"
board[guess_row][guess_col] = "X"
print_board(board)

#this is used to end the game after 4 tries
if turn == 3 :
print "You lost"
else:
print "Exiting game"
``````

#2

Try not to nest different things so much, for example nearly the whole code in in the body of an if-statement. There’s far too much code in your try-statement as well, it should usually be used to attempt a single action.

A while-loop isn’t a function, it is a statement.

For verifying input you might create a separate function that contains a loop which keeps asking until valid input is received which is then returned. You might use a try statement for attempting to convert the string to int, then check the range, return the result if it’s valid or otherwise let the loop continue.
The main reason for putting this in its own function is to avoid cluttering the main loop with all that validation code, better to just have something like: `row, col = get_input()`, possibly passing the board as an argument so that this function can use that to figure out the minimum and maximum valid values for row and col

#3

Thank you. I will do it. Have a nice day

#4

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