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.