2016-12-08 26 views
-4
#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

爲什麼你把'()'放在所有東西的周圍? – Barmar

回答

1

由於您已經擁有一組卡片(可能通過索引訪問),您可以設置一個包含值:

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

然後,如果你有兩個卡指定索引,cardOnecardTwo

cardOne = 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序列 - 只依賴數組內的數據。

+0

是的,儘管最好將它們壓縮併爲它們創建一個字典。 –

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) 
相關問題