1

It feels like a silly question, but I'm blanking on a method of converting a string to an int value, and then later on printing the actual string while it maintains the value previously assigned.

I wrote a quick example to try and explain what I'm doing; assuming there would be no equal values.

unshuffled_list = [2,3,4,5,6,7,8,9,10,'J','Q','K','A']
random.shuffle(unshuffled_list)
aDeck = []
bDeck = []
aDeck = unshuffled_list[0:26]
bDeck = unshuffled_list[26:53]

i = 0

while i <= len(aDeck):
    print("Player A: {}\nPlayer B: {}".format(aDeck[i],bDeck[i]))
    if aDeck[i] > bDeck[i]:
        print("Player A wins!\n")
    if aDeck[i] < bDeck[i]:
        print("Player B wins!\n")
    i += 1

This code breaks, no kidding, because it can't compare a string to a int, but is it possible to have this sort of print out:

Player A: 9
Player B: Q
Player B Wins!

Basically so that it prints out the actual string included in the list, but maintaining the int variable that it is assigned to.

I have gotten the piece of code to work by just using a list of int values, but for the sake of learning I wanted to know if it was possible to do so.

I also tried to mess around with the str() and int(), but didn't have any luck.

4
  • I'm not a regular Python user, so maybe I'm missing something, but what int variable are you referring to? Commented Nov 5, 2013 at 2:20
  • I suggest you define a dictionary that maps card names to values. Then compare cardValue[aDeck[i]] to cardValue[bDeck[i]]. Commented Nov 5, 2013 at 2:21
  • For instance, assign 'J' to 11, 'Q' to 12, etc. I've had it work by just putting in the variables, but printing the actual string while maintaining the int value is where I'm having an issue. Commented Nov 5, 2013 at 2:27
  • Nothing automatically evaluates strings as if they were the variables with the same name. You would have to use eval(). Commented Nov 5, 2013 at 2:29

2 Answers 2

1

Use a dictionary to map the named cards to their values.

cardValue = { 'J': 11, 'Q': '12', 'K': 13, 'A': 14}
def getCardValue(c)
    return cardValue[c] if c in cardValue else c

while i <= len(aDeck):
    print("Player A: {}\nPlayer B: {}".format(aDeck[i],bDeck[i]))
    cardA = getCardValue(aDeck[i])
    cardB = getCardValue(bDeck[i])
    if  cardA > cardB:
        print("Player A wins!\n")
    elif cardA < cardB:
        print("Player B wins!\n")
    else:
        print("It's a tie!\n")
    i += 1
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, I had just been reading up about dictionaries and didn't even think of that. Appreciate it!
Worth noting that your get card value function is just cardValue.get(c, c)
And you can use for a, b in zip(aDeck, bDeck) to make the looping a bit easier...
0

Don't worry about the integer versus string stuff. You can leave your array as-is, with mixed types. There's no need to convert string to integer or vice versa at any point.

random.shuffle(unshuffled_list) shuffles unshuffled_list in place. i.e. you lose the original sort order. So let's start by preserving a copy:

from copy import copy
shuffled_list = copy(unshuffled_list)
random.shuffle(shuffled_list)

And then replace all references of unshuffled_list with shuffled_list in your code. This also makes your code a bit more self-documenting.

Then when it comes time to compare two cards, instead of this:

if  cardA > cardB:
    ...

Do this:

if unshuffled_list.index(cardA) > unshuffled_list.index(cardB):
    ...

You can read that comparison as "if the index of cardA in the unshuffled_list is greater than the index of cardB". That'll work and hopefully that's clear. Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.