我的代码有问题。它只打印手中的两个值之一



我正在制作一个纸牌游戏,我想制作一个函数来打印手牌的值。但是,它只打印最后一张卡的值。

我试着用一个不同的函数来计算卡片的价值,这个函数用于拾取具有相同结果的卡片。

甲板和变量:

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)

卡片计数功能:

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和黑桃8,函数将值打印为8。

我想问题的发生是因为cardValue没有递增。

在您的代码中有cardValue =+ heartsCards.index(temp) + 1cardValue =+ 10不正确。

处处更改为cardValue += whatever


这是变量的增量:

a = 10
a += 100
print(a) #=> 110

这会分配值:

a =+ 1
print(a) #=> 1
# same as a = +1

相关内容

  • 没有找到相关文章

最新更新