Home > Back-end >  How do you assign a value to a string within a list?
How do you assign a value to a string within a list?

Time:10-05

I'm very new to Python, and programming all-around. My current project is creating a "bot" that I can message in command prompt or a termninal and play casino games with.

I'm having problems coding blackjack, specifically with the lists and the values of the strings within that list.


Here's what my variables & lists look like right now:

card = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
value = [1,2,3,4,5,6,7,8,9,10] #not sure what you do with this line

Here's how I choose a select a random card:

card1 = card[randint(0,12)]

And here's how I find the sum of the two randomly selected cards:

cardTotal = int(card1)   int(card2)

If I don't use "int(x)" the two numbers don't add, it just puts the two strings together, which makes sense. How do I correct this?

Your previous cards were 7 and 3. The newest dealt card is 7 for a total of 737.

The house's previous total was 7.

The house is dealt another card 4, which comes to a total of 74.

Sorry, you lost.
PS C:\Users\r\Desktop\python>
You were dealt K and 7.

Traceback (most recent call last):
  File "c:\Users\r\Desktop\python\CasinoBot.py", line 66, in <module>
    cardTotal = int(card1)   int(card2)     # Sum of both cards
ValueError: invalid literal for int() with base 10: 'K'
PS C:\Users\r\Desktop\python> 

CodePudding user response:

Not sure the actual value of the cards, so I set ace to 1, jack to 11, queen to 12, and king to 13. Change the values in the dictionary and add other text as you need. This just prints the sum of the two cards. Your problem was that if it chose a card that was not a number it wouldn't work, etc it doesn't make sense to add 'Jack' to 11 and get a number.

import random

card = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
cards = {
    'Ace': '1',
    'Jack': '11',
    'Queen': '12',
    'King': '13'
}
card1 = random.choice(card)
if card1 in cards:
    card1 = cards[card1]
card2 = random.choice(card)
if card2 in cards:
    card2 = cards[card2]

cardTotal = int(card1)   int(card2)
print(cardTotal)

CodePudding user response:

You can try dictionary. Try this :

from random import randint

card = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
special_cards = {
'Ace': '1',
'Jack': '22',
'Queen': '33',
'King': '44'
}
card1 = card[randint(0, 12)]
card2 = card[randint(0, 12)]
if card1 in special_cards.keys():
    card1 = special_cards[card1]

if card2 in special_cards.keys():
    card2 = special_cards[card2]

cardTotal = int(card1) int(card2)

print(cardTotal)

CodePudding user response:

You can assign values to elements of a list by enumerating the list.

    cards = [
        "Ace", "1", "2", "3", "4", "5", "6", "7", 
        "8", "9", "10", "Jack", "Queen", "king"]

    # select random card
    card = random.choice(cards)

    # Get value of selected card
    card_val = 0
    for i, j in enumerate(cards, 1):
        if j == card:
            card_val = i

However, this method is flawed for your use case. I believe Jack", "Queen", and "King" cards are all supposed to have some special values in Blackjack, which do not correpsond to values from the enumeration. Also, having to create an enumeration everytime you want to make a selection is noisy and inefficient.

I would suggest you use a list of tuples.

    cards = [
        ("Ace", 1), ("1", 1), ("2", 2), ("3", 3), 
        ("4", 4), ("5", 5), ("6", 6), ("7", 7), 
        ("8", 8), ("9", 9), ("10", 10), ("Jack", 10), 
        ("Queen", 10), ("King", 10)]

    # Select a random card
    card = random.choice(cards)

    # Get string representation of selected card
    card_string = card[0]

    # Get value of selected card
    card_value = card[1]

    # The sample game run shown in your question might look like this
    card1 = random.choice(cards)
    card2 = random.choice(cards)
    card_total = card1[1]   card2[1]  

CodePudding user response:

Everyone has an approach. This one uses the get method of dictionaries that allows a default option if not found. In this case, I simply return the value itself:

import random

card_faces=["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
card_values={"Ace":1,"Jack":10,"Queen":10,"King":10} # special cases

for _ in range(20):   # some examples
    card = random.choice(card_faces)
    card_value = int(card_values.get(card,card))
    print (f"Card {card}, value {card_value}")
  • Related