0

I'm using python to write a programme for poker following a course on Udacity. One of the definitions i'm using is the following which i'm using from the course.

def card_ranks(cards):
"Return a list of the ranks, sorted with higher first."
    ranks = ['--23456789TJQKA'.index(r) for r,s in cards]
    ranks.sort(reverse=True)
    return ranks

So this is meant to for example cards_ranks([TH,9C,9D,7S]) should return [10,9,9,7]. However i'm getting the following error

ValueError                                Traceback (most recent call 
last)
<ipython-input-13-ff8fb242bb41> in <module>()
----> 1 card_ranks(['9H','8C','7C','10D'])

<ipython-input-12-7eaf1ab781bc> in card_ranks(cards)
      1 def card_ranks(cards):
      2     "Return a list of the ranks, sorted with higher first."
----> 3     ranks = ['--23456789TJQKA'.index(r) for r,s in cards]
      4     ranks.sort(reverse=True)
      5     return ranks

ValueError: too many values to unpack

Does anyone know how to fix it? Btw i'm new to list comprehension i did try the following myself which works if only numbers in the cards are used

def card_ranks(cards):
    "Return a list of the ranks, sorted with higher first."
    ranks = [r[0] for r in cards]
    ranks.sort(reverse=True)
    return ranks

So naturally i tried

def card_ranks(cards):
        "Return a list of the ranks, sorted with higher first."
         ranks = ['--23456789TJQKA'.index(r[0]) for r in cards]
         ranks.sort(reverse=True)
         return ranks    

But that also did not work. I would really appreciate any help, the code is meant to work but it does not when i'm using it. I'm using a Jupyter notebook.

Pavan Sangha
  • 249
  • 1
  • 3
  • 10
  • how are you calling `card_ranks`? – Mike Tung Oct 03 '17 at 17:23
  • 2
    Hint: when the string is "10D", how is Python supposed to decide how to split that up into `r` and `s`? – Kevin Oct 03 '17 at 17:24
  • `cards` is a list, so you can only have one variable iterate over it. You cannot split it at the same time into characters like that. Also, the problem with `r[0]` is that this is just the `'1'` for the value `'10D'`. You will have to add a bit more logic on splitting those values. – poke Oct 03 '17 at 17:25
  • I think this would work, if you use `'TD'` instead of `'10D'`. – John Gordon Oct 03 '17 at 17:27
  • You should try to use another data-type instead of *string* to represent your cards. `str` objects are meant to represents strings, not cards. This is a natural case for a class, or using `Enums` to represents suits etc. Check out [this implementation I made for another question](https://stackoverflow.com/a/42399384/5014455) – juanpa.arrivillaga Oct 03 '17 at 17:33

3 Answers3

3

The value '10D' is a sequence of three characters, which is too many values to unpack into r,s.

It should work if you use 'TD' instead.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
1

Last code you did is right. But you have a small mistake. Looks like you have '10D' card. So when you execute r[0] you get '1'. But you don't have this case in your index string. So '--23456789...'.index(r[0]) rise an exception.

Right code will be something like this*:

def card_ranks(cards):
    "Return a list of the ranks, sorted with higher first."
     ranks = ['--234567891TJQKA'.index(r[0]) for r in cards]
     ranks.sort(reverse=True)
     return ranks 

* (depends on which index should have 10 card and which another cards (T, J, Q, K, A)

1

You are trying to unpack into too few variables.

In the list comprehension:

ranks = ['--23456789TJQKA'.index(r) for r,s in cards]

You are unpacking each card in cards into the variables r and s. To simplify this, you can just assign variables in the same way:

a, b = "xy"

so now a is 'x' and b holds y.

However, your problem is arising from the last card in the list: 10D. You are trying to unpack this card with 3 chars into only 2 variables. This can again be simplified as trying to do:

a, b = "10D"

which throws the same ValueError you got:

ValueError: too many values to unpack (expected 2)

which actually makes complete sense when you understand what you are trying to get the computer to do.

To fix this error, simply pass in the card value as TD instead of 10D which will allow it to be unpacked correctly into the 2 variables: r and s:

r, s = "TD"

# r <-- "T"
# s <-- "D"

This should get rid of your error and the function will output correctly. :)

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54