書籍でpythonを勉強しているのですはが、トランプゲームの戦争について書かれており
そのカードを表すCardクラスのところで大小比較のところで一部分疑問に思ったので質問しました。
Cardクラスのソースは以下です。
python
1class Card: 2 suits = ["spades","hearts","diamonds","clubs"] 3 values = [None,None, 4 "2","3","4","5","6","7","8","9","10", 5 "Jack","Queen","King","Ace"] 6 7 def __init__(self,v,s): 8 self.value = v 9 self.suit = s 10 11 def __it__(self,c2): 12 if self.value < c2.value: 13 return True 14 15 if self.value == c2.value: 16 if self.suit < c2.suit: 17 return True 18 else: 19 return False 20 return False#ここ! 21 22 def __gt__(self,c2): 23 if self.value > c2.value: 24 return True 25 26 if self.value == c2.value: 27 if self.suit > c2.suit: 28 return True 29 else: 30 return False 31 return False#ここ! 32 33 def __repr__(self): 34 v = self.values[self.value] + "of" + self.suits[self.suit] 35 return v 36 37 38card1 = Card(10,2) 39card2 = Card(11,3) 40 41print(card1 > card2) 42
「ここ!」とコメントしている行なのですが、これは
【def it(self,c2)の場合】
self.value < c2.valueかつself.value == c2.valueでない
(つまりself.value > c2.value)のときFalse
で
【def gt(self,c2)の場合】
self.value > c2.valueかつself.value == c2.valueでない(
つまりself.value < c2.value)のときFalse
という意味でしょうか?

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/25 14:18