gpt4 book ai didi

python - 初学者 : Python Value error: invalid literal for int()

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:39 24 4
gpt4 key购买 nike

我在 python 中制作了一个简单的 blackjack 程序,但是我得到了一个“ValueError:invalid literal for int() with base 10: ...”为了获得玩家手中的总值(value),在创建卡片对象之后,我尝试获得卡的等级: rank1 = Card.Card.getRank(card1)

这是类方法:

 def getRank(self):
if self.__rank == ('J'):
self.__rank = 10
return self.__rank
elif self.__rank == ('Q'):
self.__rank = 10
return self.__rank
elif self.__rank == ('K'):
self.__rank = 10
return self.__rank
elif self.__rank == ('A'):
self.__rank = 11
return self.__rank
else:
self.__rank = self.__rank
return int(self.__rank)`

它唯一一次返回 ValueError: invalid literal for int() with base 10 是如果等级是 'Q' 或 'K',它为 'J' 返回 10,为 'A' 返回 11。我不明白为什么它会为 'Q' 或 'K' 返回错误,因为 'J' 和 'A' 的代码相同......任何帮助将不胜感激......如果有帮助,在此之前我有

全类同学

#Card class

#Class card holds ranks and suits of deck
#

TEN = 10
FOUR = 4

class Card():
#Create rank list
RANK= ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]*FOUR


#Create list with rank names
rankNames=[None, 'Ace', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']


#Create suit list
suitNames = ['CLUBS','DIAMONDS', 'HEARTS', 'SPADES']


#Takes in rank, suit to create a deck of cards
def __init__(self, rank, suit):
self.__rank = rank
self.__suit = suit


#Returns the rank of card
def getRank(self):
if self.__rank == ('J'):
print (repr(self.__rank))
self.__rank = 10
return self.__rank
elif self.__rank == ('Q'):
self.__rank = 10
print (repr(self.__rank))
return self.__rank
elif self.__rank == ('K'):
print (repr(self.__rank))
self.__rank = 10
return self.__rank
elif self.__rank == ('A'):
print (repr(self.__rank))
self.__rank = 11
return self.__rank
else:
self.rank = self.__rank
print (repr(self.__rank))
return int(self.__rank)


#Returns suit of card
def getSuit(self):
return self.__suit


#Returns number of points the card is worth
def BJVaue(self):
if self.rank < 10:
return self.rank
else:
return TEN


def __str__(self):
return "%s of %s" % ([self.__rank], [self.__suit])

这是我创建卡片对象的地方

#Create a player hand                
player = []
#Draw two cards for player add append
player.append(drawCard())
player.append(drawCard())

#Display players cards
print ("You currently have:\n" , player)

#Get the rank of the card
card1 = player[0]
card2 = player[1]

#Update players card status
print (card1)
print (card2)

#Get the total of the hand
rank1 = Card.Card.getRank(card1)
rank2 = Card.Card.getRank(card2)

#Get the ranks of players cards
playerRank = [rank1 , rank2]

#Get total of players hand
totalPlayer = getTotal(playerRank)

#Display players total
print ("Your current total is: ", totalPlayer)

getTotal 函数

def getTotal(rank):
#Create and set accumulator to 0
total = 0

#for each value in the rank
for value in rank:
#add to total
total += value

#Return total
return total

希望对你有帮助

最佳答案

这条线不对:

  if self.__rank == ('J' or 'Q' or 'K'):

('J' or 'Q' or 'K') 的计算结果为 'J',所以这一行只是检查 self.__rank == 'J'.

你实际上想要:

  if self.__rank in ('J', 'Q', 'K'):

我认为您的第一个代码示例应该可行。您确定您确实在运行新代码吗?如果您尝试将同一模块导入到正在运行的 Python 实例中,它不会获取更改。此外,如果您重新定义一个类,现有实例仍将具有旧方法实现。

关于python - 初学者 : Python Value error: invalid literal for int(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4424871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com