如何为列表中的字符串赋值?

2024-10-02 12:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,而且编程非常全面。我当前的项目创建一个“机器人”,我可以在命令提示符或终端中发送消息,并与之玩赌场游戏

我对21点编码有问题,特别是列表和列表中字符串的值


以下是我的变量&;现在的列表如下所示:

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

以下是我如何选择一张随机卡:

card1 = card[randint(0,12)]

下面是我如何找到随机选择的两张卡的总和:

cardTotal = int(card1) + int(card2)

如果我不使用“int(x)”,这两个数字不会相加,它只是将两个字符串放在一起,这很有意义。我如何纠正这个问题

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> 

Tags: ofthe字符串you列表withlinecard
3条回答

我不确定这些牌的实际价值,所以我把ace设为1,杰克设为11,皇后设为12,国王设为13。更改字典中的值,并根据需要添加其他文本。这只是打印两张卡的总和。你的问题是,如果它选择了一张不是数字的卡,它将不起作用,等等,在11上加上“Jack”并得到一个数字是没有意义的

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)

你可以试试字典。 试试这个:

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)

可以通过枚举列表为列表的元素赋值

    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

然而,这种方法对于您的用例来说是有缺陷的。我相信Jack、Queen和King卡在Blackjack中都应该有一些特殊的值,这些值与枚举中的值不对应。此外,每次要进行选择时都必须创建枚举既嘈杂又低效

我建议您使用元组列表

    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]  

相关问题 更多 >

    热门问题