Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将字符串值转换为int值_Python_Python 3.x - Fatal编程技术网

如何在python中将字符串值转换为int值

如何在python中将字符串值转换为int值,python,python-3.x,Python,Python 3.x,我正在尝试用python制作一个21点游戏。就像7s值是7,但是jacks值是10,所以: cards = ['ace','king','queen'....'3','2' firstCard = random.choice(cards) secondCard = random.choice(cards); remove.cards(firstCard) print(int(firstCard) + int(secondCard)) 这对数字来说是完美的 我怎样才能为王牌或王牌做到这一点

我正在尝试用python制作一个21点游戏。就像7s值是7,但是jacks值是10,所以:

cards = ['ace','king','queen'....'3','2'

firstCard = random.choice(cards)

secondCard = random.choice(cards); remove.cards(firstCard)

print(int(firstCard) + int(secondCard))
这对数字来说是完美的
我怎样才能为王牌或王牌做到这一点…

你可以使用字典,键是“王牌”、“王牌”、“皇后”,值是相应的数值。根据游戏规则,可以根据需要映射关键点和值

mydict = {"ace": 1, "king": 13, "queen": 12}

num_of_queen = mydict["queen"]  # gets the numeric value

您可以使用字典,键是“ace”、“king”、“queen”,值是相应的数值。根据游戏规则,可以根据需要映射关键点和值

mydict = {"ace": 1, "king": 13, "queen": 12}

num_of_queen = mydict["queen"]  # gets the numeric value

使用字典会有帮助

cards =  {1:"ace": 1, 13:"king", 12:"queen"}

firstCard = random.choice(cards.keys())

secondCard = random.choice(cards.keys()); 

remove.cards(firstCard)

print(firstCard + secondCard)
要获取卡的名称,您可以:

cards[firstCard]

使用字典会有帮助

cards =  {1:"ace": 1, 13:"king", 12:"queen"}

firstCard = random.choice(cards.keys())

secondCard = random.choice(cards.keys()); 

remove.cards(firstCard)

print(firstCard + secondCard)
要获取卡的名称,您可以:

cards[firstCard]

我建议在类中封装一个常规词典,使其更易于使用:

class BlackJackDeck:
    class NoSuchCard(Exception):
        pass

    values = {'king': 10,
              'jack': 10,
              'queen': 10,
              'ace-high': 11,
              'ace-low': 1}
    values.update(zip(range(2, 11), range(2, 11)))

    def __getitem__(self, item):
        try:
            item = int(item)
        except (ValueError, TypeError):
            item = item.lower()

        try:
            return self.values[item]
        except KeyError:
            raise self.NoSuchCard
演示:


这允许您按整数或字符串键查找卡片,不关心大小写,并在卡片不存在时引发有意义的异常。

我建议在类中封装一个常规字典,使其更易于使用:

class BlackJackDeck:
    class NoSuchCard(Exception):
        pass

    values = {'king': 10,
              'jack': 10,
              'queen': 10,
              'ace-high': 11,
              'ace-low': 1}
    values.update(zip(range(2, 11), range(2, 11)))

    def __getitem__(self, item):
        try:
            item = int(item)
        except (ValueError, TypeError):
            item = item.lower()

        try:
            return self.values[item]
        except KeyError:
            raise self.NoSuchCard
演示:


这允许您按整数或字符串键查找卡片,不关心大小写,并且在卡片不存在时引发有意义的异常。

您好,欢迎使用StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。另请参阅Hello和欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。另请参见