Why does .index() not work in #5 high card, games of chance

Hello Everyone,

I’m working on #5 of games of chance in the python practice exercises, high card.
My question is that I’m trying to find the index position so that I can do a gt or lt comparison to get to the answer.
I haven’t defined my function yet, just writing up the code to see if the way I’m trying is going to work.

card_suit = ["Hearts", "Diamonds", "Spades", "Clubs"]
card_size = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
deck = []
for i in card_suit:
    for n in card_size:
        tmp_deck = (str(n) + ' of ' + i)
user, comp = random.choices(deck, k=2)

print("Player 1 has a " + user)
print("Player 2 has a " + comp)

if card_size.index(user.split()[0]) > card_size.index(comp.split()[0]):
    print("Player 1 wins")
elif card_size.index(user.split()[0]) < card_size.index(comp.split()[0]):
    print("Player 2 wins")

prints out

    if card_size.index(user.split()[0]) > card_size.index(comp.split()[0]):
ValueError: '8' is not in list
Player 1 has a Jack of Clubs
Player 2 has a 8 of Spades

My question is ‘why is the 8 not in the list’? I can print out the list and see that it’s there, yet the function is not returning it.
If I walk through the card_size line in the python shell, x = user.split()[0] and then put it into the card_size.index(x) and I get the position of the card so I can have a gt, lt comparison.
If I do the same thing in my function I get the ValueError.
Is this a timing issue?
When looking online for an answer, stackoverflow, is basically saying that I’m pulling the “8” out of the list and that’s why it can’t be found.
I’m not removing anything from the list so I’m kind of lost.
Any help would be great.

Thank you.



tried it, doesn’t help.
when I have checked with the type builtin, it’s a string even without the quotes.

In [145]: for i in card_suit: 
     ...:     for n in card_size: 
     ...:         tmp_deck = (str(n) + ' of ' + i) 
     ...:         deck.append(tmp_deck) 

In [146]: user, comp = random.choices(deck, k=2)                                                                                                                        

In [147]: x = user.split()[0]                                                                                                                                           

In [148]: type(x)                                                                                                                                                       
Out[148]: str

i’ve even tried to have it as x = int(user.split()[0], and if it’s a number, as 2, 3, 4… it gives the position in the list, yet as soon as …

In [153]: print(user, comp)                                                                                                                                             
Ace of Clubs 5 of Diamonds

In [154]: x = int(user.split()[0])                                                                                                                                      
ValueError                                Traceback (most recent call last)
<ipython-input-154-54ba9164c40b> in <module>
----> 1 x = int(user.split()[0])

ValueError: invalid literal for int() with base 10: 'Ace'

if I take the time in the python console to type everything out it will work, whether I assign it as a str or int. (for the numbers anyway)
I could put a try statement in as a possible work around so that the actual strings don’t mess with me, lot’s of extra typing, trying to make things small.
maybe a time.sleep() could help?
import time
before trying to do the index and it’s the same ValueError … not in list

Yes, this is a string because you’ve already turned it into a string up here:

The problem is when you’re using .index() and giving it a string instead of an int for 2 through 10.

You tried converting the strings of 2 through 10 into ints, but discovered you can’t call int() on Jack through Ace.

So, what else can you do to make the types match between your card_size list and your user/comp variables? (hint: you don’t need to get as complex as a try statement or time.sleep().)

When I type it out in the python console, it does not matter if the 2 - 10 is an int or a str.
That’s what’s got me messed up. (confused)

I had it working at one time, that a first time through would print out the winner, and then I reran it and I got the ValueError again. So it didn’t make sense to me.

I’m going to mess with it some more, putting the first two problems into def functions to try and give my brain a break.

I’ll be back. Thank you.

if you look for a string you won’t find an int. pretty straight-forward, I think.

I don’t know if this is what you meant, yet I have it working and printing how I would like it to.

sized_cards = enumerate(card_size)

for x, y in sized_cards:
    if user.split()[0] in str(y):
        player_1 = x
    if comp.split()[0] in str(y):
        player_2 = x
if player_1 > player_2:
    print("Player 1 wins")
elif player_1 < player_2:
    print("Player 2 wins")

Player 1 has a 9 of Diamonds
Player 2 has a 4 of Diamonds
Player 1 wins

Player 1 has a 10 of Spades
Player 2 has a 10 of Clubs

if anyone is curious about why I have str(n) here ->

for i in card_suit:
    for n in card_size:
        tmp_deck = (str(n) + ' of ' + i)

try this simple example in the python console on your computer

money = 100
print("you have " + money + " dollars.")

now replace money with str(money) in your print statement.
I needed to do that to get the formatting that I wanted.

thank you, el_cocodrilo
take care

You want one representation that you do all your logic on, and when you read input, the first thing you do is to convert to that representation. Same with when you do output, you would then convert to string for printing.

You’ll encounter the same thing again and again and again.
Convert at input/output to your internal representation, and you’re good.

It’s probably pretty common for new programmers to write code that bends over backwards in attempt to access data. The solution is to change the structure of the data so that the operations you want to do are easy.

This is an example of that:

But if you had a data type that supported ordered comparisons, that could have been:

if user > comp:

Spend some time on representation of data in your programs!

That makes a lot more sense to me than your previous post.
It cut the code down from 23 lines to 14 lines. (that part anyway)

Thank you