我正在做一个纸牌游戏,我想做一个能够打印出手中纸牌价值的功能。但是,它只会打印出最后拾取的卡的值。
我曾尝试用不同于用来提取具有相同结果的卡片的功能来计算卡片的价值。
平台和变量:
heartsCards = ["Ace of Hearts","Two of Hearts","Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts","Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts","Jack of Hearts","Queen of Hearts","King of Hearts"]
diamondsCards = ["Ace of Diamonds","Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds","Six of Diamonds","Seven of Diamonds","Eight of Diamonds","Nine of Diamonds","Ten of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds"]
clubsCards = ["Ace of Clubs","Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs","Six of Clubs","Seven of Clubs","Eight of Clubs","Nine of Clubs","Ten of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs"]
spadesCards = ["Ace of Spades","Two of Spades","Three of Spades","Four of Spades","Five of Spades","Six of Spades","Seven of Spades","Eight of Spades","Nine of Spades","Ten of Spades","Jack of Spades","Queen of Spades","King of Spades"]
yourCards = []
cardValue = 0
提取功能:
def pickUp():
randomNum = random.randint(1,4)
global cardValue
if randomNum == 1:
temp = random.choice(heartsCards)
if heartsCards.index(temp) < 10:
cardValue =+ heartsCards.index(temp) + 1
else:
cardValue =+ 10
yourCards.append(temp)
heartsCards.remove(temp)
if randomNum == 2:
temp = random.choice(diamondsCards)
if diamondsCards.index(temp) < 10:
cardValue =+ diamondsCards.index(temp) + 1
else:
cardValue =+ 10
yourCards.append(temp)
diamondsCards.remove(temp)
if randomNum == 3:
temp = random.choice(clubsCards)
if clubsCards.index(temp) <10:
cardValue =+ clubsCards.index(temp) + 1
else:
cardValue =+ 10
yourCards.append(temp)
clubsCards.remove(temp)
if randomNum == 4:
temp = random.choice(spadesCards)
if spadesCards.index(temp) < 10:
cardValue =+ spadesCards.index(temp) + 1
else:
cardValue =+ 10
yourCards.append(temp)
spadesCards.remove(temp)
cardCount函数:
def cardCount():
temp = 0
global cardValue
for card in yourCards:
if card.count("Ace"):
temp =+ 1
if temp == 0:
print ("The value of your cards is",str(cardValue) + ".")
if temp == 1:
print ("The value of your cards is",cardValue,"or",str(cardValue + 10) , ".")
if temp == 2:
print ("The value of your cards is",cardValue,"or",str(cardValue + 10),"or",str(cardValue + 10) , ".")
用两张卡片组调用cardCount()之后,唯一显示的值是后一张卡片的值。例如带有2个Hearts和8个Spades的套牌,该函数将值打印为8。
答案 0 :(得分:1)
我想问题是因为cardValue没有增加。
您的代码中有cardValue =+ heartsCards.index(temp) + 1
或cardValue =+ 10
这是不正确的。
将各处更改为cardValue += whatever
a = 10
a += 100
print(a) #=> 110
这将分配值:
a =+ 1
print(a) #=> 1
# same as a = +1