-4
def get_card_value(s):
    s = ' '
    cards ={'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}
    for s in cards.keys():
        return cards.values(s)
    else:
        raise ValueError("No card found")

get_card_value(A)

I am making a blackjack game and am stuck on the first part where I need to define a function that gets the integer points for the card s. So input is the string value of card and the output should be the integer score of the card.

Can anyone help with what I've written?

martineau
  • 119,623
  • 25
  • 170
  • 301
Cindy
  • 425
  • 1
  • 3
  • 10
  • 2
    I'd take a close look at all the [different operations dictionaries support](https://docs.python.org/3/library/stdtypes.html#typesmapping). Compare those with what you've written. Do you see anything in those operations that might work better for what you are trying to do? – Martijn Pieters Feb 04 '17 at 22:03
  • One thing to consider is that you are overwriting the value of `s` by assigning it an empty string. – José Sánchez Feb 04 '17 at 22:05
  • 1
    @José: Actually it's assigning a string with one space character in it to `s`—but otherwise the point you make is accurate. In addition, the card rank argument passed to the function should be a string: i.e. `get_card_value('A')` and the membership test should just be `for s in cards:`. – martineau Feb 04 '17 at 22:08
  • Hint: what's `cards['A']` going to equal in the function? – ernie Feb 04 '17 at 22:11
  • It says name 'A' is not defined – Cindy Feb 04 '17 at 22:21
  • Possible duplicate of [Sort a Python dictionary by value](http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – Cindy Feb 07 '17 at 21:52

2 Answers2

0

Accessing values in a dictionary is not done via the values() method, but rather via bracket-notation (eg. my_dictionary[key]). See d[key] in the documentation.


Additionally, you could try just making a dict instead of a full-blown function:

>>> card_points = {'A': 1, '2': 2, '3': 3, '4': 4, '5':5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}
>>> card_points['A']
1
>>> card_points['5']
5

If you need it to be a function you can easily wrap this dictionary:

def get_card_value(s):
    card_points = {'A': 1, '2': 2, '3': 3, '4': 4, '5':5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}
    try:
        return card_points[s]
    except KeyError:
        raise ValueError("No card found")

Note: KeyError is the exception raised when trying to access a non-existent key in a dictionary, so if we encounter one of those, we can choose to instead raise our custom ValueError exception.


Or if you want to get fancy (read: save some keystrokes):

>>> card_points = dict({str(i): i for i in range(2, 11)}, A=1, J=10, Q=10, K=10)

In the snippet above, {str(i): i for i in range(2, 11)} uses a dictionary comprehension to create a dictionary mapping string values for the integers 2-10 to their numeric values. For the non-numeric cards, I pass them in as keyword arguments. Luckily, python's built-in dict constructor can handle both of these at once!

So if you are just beginning you might want to use the first form rather than leverages a bunch of python's advanced features :)

Community
  • 1
  • 1
Pedro Cattori
  • 2,735
  • 1
  • 25
  • 43
0

I recommend writing a dictionary outside the function but if you want it inside try this

def get_card_value(s): 
    cards = {'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10} 
    if s in cards:
        return cards[s]
    else: 
        raise ValueError("No card found")

and then try it like this

print(get_card_value("4"))
print(get_card_value("Y"))

Keys in dictionary are strings so you have to use function get_card_value using strings