Python 如何更改代码,以便jack、queen等将我的总数增加10
因为您已经有了一个卡数组(可能通过索引访问),所以您可以设置一个并行数组来保存以下值:Python 如何更改代码,以便jack、queen等将我的总数增加10,python,python-3.x,random,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','J
#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)
然后,如果您有两张指定索引的卡片,cardOne
和cardOne
:
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]
这样,您就不必编写大量笨拙的
if
序列,只需依赖数组中的数据即可。您需要将random.choice()
的结果分配给变量,以便进行比较。您正在比较卡片
,这是所有卡片的列表,而不仅仅是随机选择的卡片
那么您的if
声明对于皇家卡是错误的。您应该测试的是card=='Jack'
,而不是card==(Jack)
,因为后者是保存要添加的值的变量,而不是cards
数组中的字符串
但是,与其使用所有那些if/elif
语句,不如使用字典将卡名映射到它们的值
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])
你为什么要把
()
放在每件东西的周围?是的,不过最好把它们拉上拉链,为它们创建一个dict。
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)