【发布时间】:2016-04-15 04:02:58
【问题描述】:
我试图弄清楚如何将 t、j、q 和 k 的值设置为 int 的 10 值。有人可以解释我哪里出错了吗?
class Card:
def __init__(self, value , suit):
self.value = value
self.suit = suit
def __repr__(self):
return "The " + self.value + " of " + self.suit
def intValue(self):
if int(self.value) > 2 or int(self.value) < 9:
return self.value
elif str(self.value) == 'a':
return 1
elif str(self.value) == 'j' or str(self.value) == 'q' or str(self.value) == 'k' or str(self.value) == 't':
return 10
【问题讨论】:
-
>2和<9省略了 2 和 10,我很确定它们是有效的卡片。无论如何,您必须先告诉我们出了什么问题,然后我们才能考虑哪里您出了问题。 -
我猜最后一行的
t是 10...? -
IMO 没有理由为此创建一个类,而是使用一个元组...
(value, suit)。然后是dict、{'a': 1, 'j': 10, 'q': 10, 'k': 10, 't': 10}。 -
如果
self.value类似于'j'、'q'等,则不能尝试执行int(self.value)。您必须检查它是否为非数字之前 试图将其传递给int()。否则你会得到一个错误,正如你所期望的(你期望int('k')做什么?) -
一张 ace 的值是 1 还是 11,取决于总数?