I am trying to write a function that is supposed to accept a dictionary argument to validate a Chessboard. According to the grid of a "Valid" board there are 64 possible "valid" key values that can be assigned in the dictionary.
Those are columnwise 1- 8 which I have already addressed a check for with int(key[0:-1]) > 8
. How can I loop through the rows of the keys which a - h are the valid ranges?
here are some sample keys to give an example of the grid of acceptable key values: 1c, 2g, 8d, 4f, etc up to a maximum of 8h.
A friend suggested startswith()
, which would be fine, but I don't know how to format that to compare the key values with startswith()
. I'd like to know how I can loop through letters a, b, c, etc. As easily as you can with something like range()
for numbers.
Also how could I do a compare with letters like if key[-1] > 'h'? or is that even possible?
def cBV(dic): # (c)hess(B)oard(V)alidator
if not isinstance(dic, type({})):
print('TypeError: object passed to cBV is not of type <class dict>')
return 'VOID'
if 'wking' and 'bking' not in dic.values():
return False
for key in (dic):
try:
int(key[0:-1]) > 8
print(key) # to show me the "valid" key
except ValueError:
print(key) # just a personal validation check
return False
return True
dict = {'8h': 'wking', '2fc': 'bking'} # key value 2fc intentionally set to fail
test = cBV(dict)
print(test)
Here is the full code. I'm reading over that ty its a bit complex to me may take some time