I am going nuts with this. I am doing the 13. Battleship-Game. I already finished all the given tasks here and want to add now some extra functionality.
My game allows you to try 4 times to sink the battleship which is set by the for loop with range(4). I want that in case you either put in a value that is a) out of range or b) has been given already the "try" doesnt count. I want that only the tries which either result in a miss or victory will count. I want to have a maximum of 4 misses but endless of the above listed a) and b) results. They just shouldn't count.
I tried already to "deduct a turn" with "turn -1" or "turn = turn -1" but it doesnt help. Another idea was to use the "continue" statement, which as far as i understand skips the current part and continues afterwards with the iteration. But nothing worked. I can still give 4 times values outside of the board and the game ends after the 4th try.
I would really appreciate any input. Please see my code below. I left the parts of code I tried already out but added a comment instead.
#random number for battleship placement from random import randint board =  for x in range(5): board.append(["O"] * 5) #remove " " between "O"'s in board def print_board(board): for row in board: print " ".join(row) print "Let's play Battleship!" print_board(board) #define position of battleship 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) #four times user input for turn in range(4): guess_row = int(raw_input("Guess Row:")) -1 guess_col = int(raw_input("Guess Col:")) -1 #different messages depending on the output if guess_row == ship_row and guess_col == ship_col: print "Congratulations! You sunk my battleship!" #get out of the loop earlier if battleship sunk but guesses are left break else: if guess_row not in range(len(board)) or guess_col not in range(len(board)): print "Oops, that's not even in the ocean." #i tried here already "turn -1", "turn = turn-1", "continue" to discount this from my 4 tries elif(board[guess_row][guess_col] == "X"): print "You guessed that one already." #i tried here as well "turn -1", "turn = turn-1", "continue" to discount this from my 4 tries elif turn == 3: print "Game Over" else: print "You missed my battleship!" board[guess_row][guess_col] = "X" #print each turn print "Turn", turn +1 print_board(board)