我的代码有问题。它只打印手中的两个值之一
I'm having a problem with my code. It only prints one of the two values in the hand
我正在制作一个纸牌游戏,我想制作一个函数来打印手中纸牌的价值。但是,它只打印最后一张拿起的牌的价值。
我尝试用不同的函数来计算卡片的价值,而不是用来计算卡片的价值,结果相同。
套牌和变量:
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 和黑桃 8 的牌组,该函数将值打印为 8。
我想问题的发生是因为 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
我正在制作一个纸牌游戏,我想制作一个函数来打印手中纸牌的价值。但是,它只打印最后一张拿起的牌的价值。
我尝试用不同的函数来计算卡片的价值,而不是用来计算卡片的价值,结果相同。
套牌和变量:
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 和黑桃 8 的牌组,该函数将值打印为 8。
我想问题的发生是因为 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