我想在Python 3中製作一個二十一點模擬器,並且試圖定義一個函數來爲它處理的卡片分配一個數值。我相信我在調用我的函數時犯了一個錯誤。通過在Python中調用一個函數將一個數值賦給一個字符串3
{
import random
import CardVal
cards = [
'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS',
'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH'
]
decks = 6 * cards
player = []
dealer = []
for i in range(2):
player.append(decks.pop(random.randrange(len(decks) - 1)))
dealer.append(decks.pop(random.randrange(len(decks) - 1)))
print('Players cards:', player)
print('Dealers card:', dealer[1])
total = 0
for i in range(len(player)):
CardVal.card_value(player[i])
total += player[i]
}
,這裏是功能我打電話
{
def card_value(card):
if '2' in card:
card = 2
elif '3' in card:
card = 3
elif '4' in card:
card = 4
elif '5' in card:
card = 5
elif '6' in card:
card = 6
elif '7' in card:
card = 7
elif '8' in card:
card = 8
elif '9' in card:
card = 9
elif 'T' in card or 'J' in card or 'Q' in card or 'K' in card:
card = 10
elif 'A' in card:
card = 11
}
使用字典而不是... – Idos
你得到什麼輸出?你期望得到什麼? –
大括號是什麼? – user2357112