[英]Creating a basic Blackjack game in Python - struggling to assign and total card values
I apologise in advance for absolute novice I am. 对于绝对的新手,我事先表示歉意。
So this was my attempt at assigning values to each card (the program is picking from a separate .txt file that has the list of cards in it) 因此,这是我为每个卡分配值的尝试(程序从其中包含卡列表的单独的.txt文件中进行选择)
cardAvalue = int
if cardA == 'Jack of Spades' or 'Queen of Spades' or 'King of Spades' or '10 of Spades' or 'Jack of Hearts' or 'Queen of Hearts' or 'King of Hearts' or '10 of Hearts' or 'Jack of Clubs' or 'Queen of Clubs' or 'King of Clubs' or '10 of Clubs' or 'Jack of Diamonds' or 'Queen of Diamonds' or 'King of Diamonds' or '10 of Diamonds':
cardAvalue = int(10)
elif cardA == '2 of Spades' or '2 of Hearts' or '2 of Clubs' or '2 of Diamonds':
cardAvalue = int(2)
elif cardA == '3 of Spades' or '3 of Hearts' or '3 of Clubs' or '3 of Diamonds':
cardAvalue = int(3)
elif cardA == '4 of Spades' or '4 of Hearts' or '4 of Clubs' or '4 of Diamonds':
cardAvalue = int(4)
elif cardA == '5 of Spades' or '5 of Hearts' or '5 of Clubs' or '5 of Diamonds':
cardAvalue = int(5)
elif cardA == '6 of Spades' or '6 of Hearts' or '6 of Clubs' or '6 of Diamonds':
cardAvalue = int(6)
elif cardA == '7 of Spades' or '7 of Hearts' or '7 of Clubs' or '7 of Diamonds':
cardAvalue = int(7)
elif cardA == '8 of Spades' or '8 of Hearts' or '8 of Clubs' or '8 of Diamonds':
cardAvalue = int(8)
elif cardA == '9 of Spades' or '9 of Hearts' or '9 of Clubs' or '9 of Diamonds':
cardAvalue = int(9)
elif cardA == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds'):
cardAvalue = int(11)
cardCvalue = int
if cardC == 'Jack of Spades' or 'Queen of Spades' or 'King of Spades' or '10 of Spades' or 'Jack of Hearts' or 'Queen of Hearts' or 'King of Hearts' or '10 of Hearts' or 'Jack of Clubs' or 'Queen of Clubs' or 'King of Clubs' or '10 of Clubs' or 'Jack of Diamonds' or 'Queen of Diamonds' or 'King of Diamonds' or '10 of Diamonds':
cardCvalue = int(10)
elif cardC == '2 of Spades' or '2 of Hearts' or '2 of Clubs' or '2 of Diamonds':
cardCvalue = int(2)
elif cardC == '3 of Spades' or '3 of Hearts' or '3 of Clubs' or '3 of Diamonds':
cardCvalue = int(3)
elif cardC == '4 of Spades' or '4 of Hearts' or '4 of Clubs' or '4 of Diamonds':
cardCvalue = int(4)
elif cardC == '5 of Spades' or '5 of Hearts' or '5 of Clubs' or '5 of Diamonds':
cardCvalue = int(5)
elif cardC == '6 of Spades' or '6 of Hearts' or '6 of Clubs' or '6 of Diamonds':
cardCvalue = int(6)
elif cardC == '7 of Spades' or '7 of Hearts' or '7 of Clubs' or '7 of Diamonds':
cardCvalue = int(7)
elif cardC == '8 of Spades' or '8 of Hearts' or '8 of Clubs' or '8 of Diamonds':
cardCvalue = int(8)
elif cardC == '9 of Spades' or '9 of Hearts' or '9 of Clubs' or '9 of Diamonds':
cardCvalue = int(9)
elif cardC == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds') and (cardAvalue <= 10):
cardCvalue = int(11)
elif cardC == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds') and (cardAvalue > 10):
cardCvalue = int(1)
cardA = cardAvalue
print(cardAvalue)
Both cardAvalue
and cardCvalue
both keep coming out as 10, the number of the variable given, despite me clearly failing to assign numerical values. 尽管我显然无法分配数值,但
cardAvalue
和cardCvalue
都保持为10,即给定变量的数量。 Sorry for the excessive int() tags given, I've been messing with my code in frustration. 很抱歉给定过多的int()标记,我很沮丧地弄乱了我的代码。
I'm trying to work out the numerical total of the player's hand. 我正在尝试计算玩家手牌的总数。 Where am I going wrong/What would be the best way of doing this?
我在哪里错了/做这件事的最好方法是什么?
You can probably do this without a function, but I have a feeling your next assignment will be to extend the functionality of the code to do something else. 您可能不需要功能就可以执行此操作,但是我感觉您的下一个任务将是扩展代码的功能以执行其他操作。 In the example below I tried to encode the rules you seem to be using.
在下面的示例中,我尝试对您似乎正在使用的规则进行编码。
score_threshold
an Ace counts for 11, else 1. score_threshold
的A计数为11,否则为1。 So then we can encode those rules in a function: 因此,我们可以将这些规则编码为一个函数:
def process_cards(cards, score_threshold):
score = 0
face_cards = ["Jack", "Queen", "King"]
for x in cards:
card = x.split()[0] # Discard suit
if card in face_cards: # Face cards are ten
score = score + 10
elif card == "Ace":
# Score based on score_threshold arg
if score <= score_threshold:
score = score + 11
else:
score = score + 1
# If it's a digit, pass through that number to score
elif card.isdigit():
score = score + int(card)
# Everything else is an error case
else:
print("Invalid card!")
# Make the score accessible outside the function
return score
p = process_cards(["Queen of Spades", "Ace of Hearts"], 9)
print(p) # 11
At then end we can assign the return
value of that function to a variable and then use it for whatever comes next. 然后,我们可以将该函数的
return
值分配给变量,然后将其用于接下来的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.