I propose to wrap a regular dictionary in a class to make it a little easier to use:
class BlackJackDeck:
class NoSuchCard(Exception):
pass
values = {'king': 10,
'jack': 10,
'queen': 10,
'ace-high': 11,
'ace-low': 1}
values.update(zip(range(2, 11), range(2, 11)))
def __getitem__(self, item):
try:
item = int(item)
except (ValueError, TypeError):
item = item.lower()
try:
return self.values[item]
except KeyError:
raise self.NoSuchCard
Demo:
>>> d = BlackJackDeck()
>>> d[2]
2
>>> d['7']
7
>>> d['KING']
10
>>> d['ace-high']
11
>>> d[100]
[...]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...", line 21, in __getitem__
raise self.NoSuchCard
d.NoSuchCard
This allows you to look up cards by integer- or by string-key, does not care about capitalization and throws a meaningful exception when a card does not exist.