0

so I am working on a personal project where I simply recreate the game mechanics of Battleship (the board game). The main issue I have encountered so far since beginning of this project is the system for raising errors; I would move onto another part of the project but I am so fixated on this.

I have tried many iterations of how this could possibly work given my limited knowledge of programming but none of it is working. This is what I've got so far:

def prompting():
    while True:
        try:
            x = input('Please select the coordinate at which you would like to begin placing the aircraft carrier (5-units):')
            break
        except ValueError:
            check_validity(x)
            if x is False:
                print('Coordinates are indicated by letter A - J on the vertical axis and 1 - 10 on the horizontal axis. Please use the values to indicate a valid coordinate:')
            
prompting()

For my project, I am using a 10 x 10 matrix to act as the board and I have a dictionary that associates the indexed positions of each element of the matrix (the keys) with an coordinate identifiers that include letter A-J on the vertical axis and 1-10 on the horizontal axis (ex A1, A2, A3...A10 - J1...J10. I have a function that checks the validity of the coordinate that the user input:

def check_validity(coordinate):   
    keys = list(coordinates_dict.keys()) 
    for i in keys:
        if coordinate in keys:
            return True
        else:
            return False 

check_validity('A11')

Essentially the function above checks if the input is found in the list and returns true or false. This function is then called upon in the prompting function to see if the input is valid and to raise an error if it is not valid and therefore prompt the user to input a valid value. The issue I am having is that it will prompt the user to input but whether I put a correct or incorrect the value, the except block is always skipped. What should I/can I do to fix this.

If this is not possible when using a try-except block, then I would also like to know how I can fine tune an if-else statement to essentially do the same thing. I have experimented with this approach a bit but I am running into the issue of the error actually being raised but it won't prompt user input after (and I assume that is because it is not being looped by a while loop or anything like that. Thank you all who help.

  • You need to assign the return value of `check_availability` to a variable used in your if statement. – BTables May 17 '23 at 18:58
  • 1
    `except ValueError` in your current code won't ever be used, because the `input` function doesn't raise ValueErrors. It's a very generic function which has no knowledge of your rules about what is valid. If you wanted, you could call `check_validity` within the try block, and make `check_validity` raise a ValueError if it finds invalid input. However, personally I'd stick with having that function return True/False. In that case, you don't need try/except. – slothrop May 17 '23 at 19:14
  • There's no need for the `for` loop in `check_validity()`. The whole function should be `return coordinate in coordinates_dict` – Barmar May 17 '23 at 19:45
  • Thank you so much for the feedback y'all, pretty much every bit of advice you all gave helped me figure it out one way or another. The `prompting()` function seems to be doing its job, the only issue is that when you are typing in invalid responses, if you do it enough times, it gets to a point where you may enter a valid coordinate and it won't register it and raise the `ValueError` but when you type in the second time it works. I am tweaking with it a bit to figure it out but if anyone has any idea then PLEASE LET ME KNOW! :) – danielpintard May 17 '23 at 21:58
  • Actually nevermind, I figured it out on my own. Once again, thank you all so much for your help. – danielpintard May 17 '23 at 22:51

0 Answers0