2

Working on a battleship game in python. My function to check the user's 'guess' input in resulting in an endless validation loop. I want a guess in the format of 'a10' on a 10x10 grid. The validation function i have built to help validate is as follows:

  def validate_guess(self,guess):
        while True:
            if (len(guess)) in range(2, 4):
                if guess[0] not in 'abcdefghij' or guess[1:] not in '1,2,3,4,5,6,7,8,9,10':
                    print("Sorry the Grid is a 10x10 square you must enter a valid position.  Try again...")
                    continue

                else:
                    return guess

            else:
                if len(guess) < 2 or len(guess) > 3:
                    print("Oops!  That's too not the right amount of characters. Please try again...")
                    continue

If the user guesses an incorrect value--it does spot it--but the error is returning a never ending loop with the printed error statement.

This is the portion of my game where the validation function is being used:

while True:
        print("\n")
        # get guess from player one in coordinate form (example: a10)
        guess = input("{}'s turn to guess: ".format(player1.player))
        # strip characters from the guess input
        guess = guess.strip()
        # validate the guess
        guess = self.validate_guess(guess)
        # append the guess to a list for player 1
        player1.guesses.append(guess)
        # break down the coordinates into x and y variables
        x, y = ship1.split_guess(guess)
        # increment guesses
        guesses += 1
        # loop to assess whether, hit, miss or a won game
        if any(guess in ship for ship in grid2.play_two_board):
            print("HIT")
            grid2.print_guess_board(x, y, "h")
            for ship in grid2.play_two_board:
                try:
                    ship.remove(guess)
                    print(len(Board.play_two_board))
                    self.check_if_sunk(ship)
                    win = self.play_one_win_check(player1.player)
                    if win == 1:
                        print ("GAVE OVER!")
                        break
                except ValueError:
                    pass
        else:
            print("Miss!")
            grid2.print_guess_board(x, y, "m")

Not sure if it might be because i have two While statements? Just really stuck here would appreciate any guidance. Thanks.

*****************************8 edit --changed function to include the guess without actually passing it that value from the input statement but still getting same problem.

  def validate_guess(self):
        guess = input("Please enter a location:")
        while True:
            if len(guess) in range(2, 4):
                if guess[0] not in 'abcdefghij' or guess[1:] not in '1,2,3,4,5,6,7,8,9,10':
                    print("Sorry the Grid is a 10x10 square you must enter a valid position.  Try again...")
                    continue

                else:
                    return guess

            else:
                if len(guess) < 2 or len(guess) > 3:
                    print("Oops!  That's too not the right amount of characters. Please try again...")
                    continue

and just calling it like this:

while True:
        print("\n")
        # get guess from player one in coordinate form (example: a10)
        # guess = input("{}'s turn to guess: ".format(player1.player))
        # strip characters from the guess input
        # guess = guess.strip()
        # validate the guess
        guess = self.validate_guess()
John Rogerson
  • 1,153
  • 2
  • 19
  • 51
  • `while True:` in `def validate_guess(self,guess):` doesn't appear to make sense. `guess` is an argument that you pass to the function. It retains the same value whilst that function runs because nothing in the function modifies or redefines it. If it doesn't satisfy the conditions to reach a `return` statement in its first pass through your checks, it never will. Your `while` loop then only serves to do the same, failing, validation checks over and over forever. – roganjosh Mar 14 '17 at 19:27
  • Possible duplicate of [Prompt the user to input something else if the first input is invalid](http://stackoverflow.com/questions/9609213/prompt-the-user-to-input-something-else-if-the-first-input-is-invalid) – Prune Mar 14 '17 at 19:29
  • thanks -- i think i understand what you are referring to--i edited code (As shown above) to include input statement in the function--but still getting the same error. – John Rogerson Mar 14 '17 at 19:47
  • never mind i got it--thank you – John Rogerson Mar 14 '17 at 19:48

1 Answers1

2

Within validate_guess, when the user enters a bad value, you have to get new input before you check again. See here.

Either return an error code (True/False ?) from your function, or have the function loop until the input is valid. Just add a command for new input at the bottom of the function's while loop.

Community
  • 1
  • 1
Prune
  • 76,765
  • 14
  • 60
  • 81
  • hmm--ok so i added the input statement within the function (without passing the guess variable) and still getting the same problem --see edited code above – John Rogerson Mar 14 '17 at 19:46
  • ahh. never mind--i didn't put input statement within the while statement--woops. thanks for you help. – John Rogerson Mar 14 '17 at 19:48