#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)
-4
A
回答
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]
然後,如果你有兩個卡指定索引,cardOne
和cardTwo
:
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)
相關問題
- 1. 我如何列出使用千斤頂的輸入設備C
- 2. 我該如何修復我的代碼,以便可以迭代浮點數?
- 3. 我該如何更改我的密碼?
- 4. 我該如何更改我的gulp文件,以便在代碼庫發生更改時運行構建任務?
- 5. 我如何更改我的C代碼?
- 6. 我女王之謎的代碼有什麼錯誤?
- 7. 如何修改我的代碼以將總計存儲到我的decArray中?
- 8. 我該如何更改舊項目,以便可以使用ARC?
- 9. 如何將此代碼添加到我已有的代碼中以便工作
- 10. 我只想要如何更改該代碼中的數量?
- 11. 我可以更改我的Mac上的iOS 7代碼,然後將其加載到我的iPhone上5s
- 12. 我該如何修復我的代碼?
- 13. 我該如何「ajaxify」我的PHP代碼?
- 14. 如何更改這2行HTML,以便我可以將它放入我的CSS?
- 15. 我可以更改我的代碼以便我的腳本運行速度更快嗎?
- 16. 如何循環此代碼,以便我可以添加到總成本值
- 17. 如何更改我的下列快速排序代碼,以便我可以使用隨機數據透視表?
- 18. 我該如何修復此代碼,以便我可以將此JFreeChart添加到面板
- 19. 我該如何改進工會迭代
- 20. 如何將我的HTML代碼更改爲ASP代碼?
- 21. 如何更改我的html代碼?
- 22. C#如何更改我的代碼?
- 23. 我該如何改進此JavaScript代碼?
- 24. 我該如何改進此代碼
- 25. 我該如何改變此Javascript代碼
- 26. 我該如何改進此代碼?
- 27. 我該如何改進這段代碼?
- 28. 如何更改我的代碼以獲取值到數組中
- 29. 如何更改我的SAS代碼以查找最大數量
- 30. 如何獲得老版本的千斤頂節點的屬性?
爲什麼你把'()'放在所有東西的周圍? – Barmar