2016-04-15 182 views
-4

我試圖找出如何使tjqk的值的10int值。有人能解釋我在哪裏出錯了嗎?酒杯的Python

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 
+4

'> 2'和'<9'省略2和10,我很確定這是有效的卡。無論如何,在我們考慮你出錯的地方之前,你必須告訴我們出了什麼問題。 – TigerhawkT3

+0

我在猜測最後一行的't'是10 ...? –

+1

海事組織沒有理由爲此做一個類,而只是使用一個元組......'(價值,訴訟)'。然後是'dict','{'a':1,'j':10,'q':10,'k':10,'t':10}'。 – Signal

回答

0

儘管我同意使用字典來表示值的意見,讓我們來解決你的問題。這主要是保持簡單和記住你的數據(STR)的類型,而不是隨機強加str()int()通話的問題時,他們並不需要:

class Card: 

    def __init__(self, value, suit): 
     self.value = value 
     self.suit = suit 

    def __repr__(self): 
     return "The {} of {}".format(self.value, self.suit) 

    def intValue(self): 
     if self.value.isdigit(): 
      return int(self.value) 
     elif self.value == 'a': 
      return 1 
     else: 
      return 10