我对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>
我不确定这些牌的实际价值,所以我把ace设为1,杰克设为11,皇后设为12,国王设为13。更改字典中的值,并根据需要添加其他文本。这只是打印两张卡的总和。你的问题是,如果它选择了一张不是数字的卡,它将不起作用,等等,在11上加上“Jack”并得到一个数字是没有意义的
你可以试试字典。 试试这个:
可以通过枚举列表为列表的元素赋值
然而,这种方法对于您的用例来说是有缺陷的。我相信Jack、Queen和King卡在Blackjack中都应该有一些特殊的值,这些值与枚举中的值不对应。此外,每次要进行选择时都必须创建枚举既嘈杂又低效
我建议您使用元组列表
相关问题 更多 >
编程相关推荐