단순히 카운터를 증가하기에 충분하다 : '! fight1 = 2' '! fight1.best = 2'당신은 당신의 확인을위한 "최고", 빨간색으로해야하는 기능을 필요
class Sparring():
def __init__(self, mywins = 0, hiswins = 0):
self.mywins = mywins
self.hiswins = hiswins
def my_score(self):
self.mywins += 1
def his_score(self):
self.hiswins += 1
@property
def best (self):
return max ([self.mywins, self.hiswins])
. 여러분의 프로그램은 읽을 수 :
def fight_match():
print "Okay you're in the ring, ready to go three rounds with this dude"
print "He throws a left hook, which way will you dodge?"
dodge = raw_input()
fight1 = Sparring()
while fight1.best != 2:
if 'right' in dodge:
print "Nice job, you knocked him out!"
fight1.my_score()
else:
print 'Oh no he knocked you out'
fight1.his_score()
또 다른 한가지는 당신이 아마 while 루프에 입력을 이동하려는 것입니다 :
이
fight1 = Sparring()
while fight1.best != 2:
dodge = raw_input()
if 'right' in dodge:
print "Nice job, you knocked him out!"
fight1.my_score()
else:
print 'Oh no he knocked you out'
fight1.his_score()
귀하의 코멘트에 대답하기 위해, 내가 샘플 implementatio를 제공합니다 전체 싸움의 : 그냥 완성도를 위해서 몇 가지 코드 골프을하기 위해
import random
class Fight:
def __init__ (self): self.scores = [0, 0]
def heScores (self): self.scores [1] += 1
def youScore (self): self.scores [0] += 1
@property
def best (self): return max (self.scores)
@property
def winner (self): return 'You win.' if self.scores [0] > self.scores [1] else 'He wins.'
fight = Fight()
print ('Fight begins.')
question, answer = 'left', 'right'
while fight.best != 2:
if random.randint (0, 1): question, answer = answer, question
if answer in input ('He throws a {} hook, which way will you dodge? '.format (question)):
print ('Nice job, you knocked him out.')
fight.youScore()
else:
print ('Oh no, he knocked you out.')
fight.heScores()
print (fight.winner)
,이 같은 않는 한 - 라이너 :
_ = [print ('Fight!'), print ('You won.' if (lambda a, b: (lambda a, *b: a (a, *b)) ((lambda a, b, c, d, e: e if max (e) == 2 else a (a, b, c, c (b), [print ('Nice job, you knocked him out.'), (1 + e [0], 0 + e [1])] [1] if d [1] in input ('He throws a {} hook, which way will you dodge? '.format (d [0])) else [print ('Oh no, he knocked you out.'), (0 + e [0], 1 + e [1])] [1])), b, a, a (b), (0, 0))) ((lambda a: ('left', 'right') if a.randint (0, 1) else ('right', 'left')), __import__ ('random')) [0] == 2 else 'He won.') ]
'self.mywins = mywins = + 1' : self.mywins 및 mywins에 1의 값을 할당하고 있습니다. 실제로 그 작업을 수행하려고합니다. ;-) – Gryphius
레프트 훅을 던지고 오른쪽으로 피하는 경우 주먹에 피하고 있지 않습니까? – user2357112