我怎么能改变我的代码,以便杰克,女王等将增加10我的总数

时间:2016-12-08 09:27:52

标签: python python-3.x random

#pontoon

import random

total = (0)

Ace = (10)

Jack = (10)

Queen = (10)

King = (10)


suits = ['Hearts','Clubs','Diamonds','Spades']#list of suits
cards = ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','king']#list of cards

print('The rules:\nYou need to get the sum of your cards as close to 21 as possible.\nAces, Jacks, Queens and Kings are all equal to 10.\nStick means that you dont want to recieve anymore cards.\nBust means that the sum of your cards has exceeded 21 so you automatically lose.\nTwist means you want another card.')
input()

random.choice (suits)
random.choice (cards)
print (random.choice (cards)+ ' of ' +random.choice (suits))#this shows a random card
print (random.choice (cards)+ ' of ' +random.choice (suits))

if cards == (Jack):
    total =+ (Jack)

elif cards == (Queen):
    total =+ (Queen)

elif cards == (King):
    total =+ (King)

elif cards == (Ace):
    total =+ (Ace)

elif cards == 2:
    total =+ (int('2'))

elif cards == 3:
    total =+ (int('3'))

elif (cards) == ('4'):
    total =+ (int('4'))

elif (cards) == ('5'):
    total =+ (int('5'))

elif (cards) == ('6'):
    total =+ (int('6'))

elif (cards) == ('7'):
    total =+ (int('7'))

elif (cards) == ('8'):
    total =+ (int('8'))

elif cards == 9:
    total =+ (int('9'))




print ('\nyour total is...')
print (total)

2 个答案:

答案 0 :(得分:1)

由于您已经拥有一系列卡片(可能是通过索引访问),您可以设置一个包含值的并行数组:

cardOne

然后,如果您有两张指定索引的卡片,cardTwocardOne = 3 # card '4' , value 4. cardTwo = 12 # card 'King', value 10. print ("Card one is ", cards[cardOne], " with value ", vals[cardOne]) print ("Card two is ", cards[cardTwo], " with value ", vals[cardTwo])

if

这样你就不必编写大量笨拙的ITrigger (interface)序列 - 只需依赖数组中的数据。

答案 1 :(得分:1)

您需要将random.choice()的结果分配给变量,以便比较它们。您正在比较cards,这是所有卡片的列表,而不仅仅是随机选择的卡片。

然后你的if陈述错误的王牌。您应该测试card == 'Jack',而不是card == (Jack),因为后者是包含您要添加的值的变量,而不是cards数组中的字符串。

但是,不是使用所有if/elif语句,而是使用字典将卡名称映射到其值。

card_values = { 'Ace': 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 }

suit = random.choice(suits)
card = random.choice(cards)
print (card + " of " + suit)
total += card_values[card]

print ('\nyour total is...')
print (total)