1

尝试创建一个简单的类来保持拳击比赛的得分,第一次在代码中实现一个类。关于课程,我是否缺少一些基本的东西?

class Sparring():
    def __init__(self, mywins, hiswins):
        self.mywins = mywins
        self.hiswins = hiswins

    def my_score(self, mywins):
        self.mywins = mywins =+ 1
        return self.mywins
    def his_score(self, hiswins):
        self.hiswins = hiswins =+ 1
        return self.hiswins

我尝试过使用 return 而不是使用它,各种组合,这是我用来调用类的函数:

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(0,0)

    while fight1 != 2:
        if 'right' in dodge:
            print "Nice job, you knocked him out!"
            fight1 = Sparring(1,0)
            fight1.my_score(1)
        else:
            print 'Oh no he knocked you out'
            fight1 = Sparring(0, 1) 
            fight1.his_score(1)
4

2 回答 2

1

每次调用它时,您都会重新初始化该类。这是一个更清洁的版本:

class Sparring():
    def __init__(self, mywins, hiswins):
        self.mywins = mywins
        self.hiswins = hiswins

    def i_win(self):
        self.mywins += 1

    def he_wins(self):
        self.hiswins += 1

    def __repr__(self):
        return "My wins: {}; his wins: {}".format(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?"

    fight = Sparring(0,0)

    dodge = raw_input()
    if 'right' in dodge:
        print "Nice job, you knocked him out!"
        fight.i_win()
    else:
        print 'Oh no he knocked you out'
        fight.he_wins(1)

    print fight

fight_match()

在这段代码中,Sparring()是一个具有两个函数的类,he_wins()并且i_win(). 如果他赢了,就跑he_wins();如果你赢了,就跑i_win()

fight_match()中,您会看到我们创建fight了 ,它是一个Sparring对象——这意味着它具有Sparring类的所有方法和变量(请记住,您不直接与类交互——您与该类的对象交互)。那么我们接受一个raw_input(), dodge。如果这个输入是“正确的”,那么我们更新战斗对象以反映我们的一场胜利(通过使用i_win())。如果不是,那么他赢了……所以我们这样做fight.he_wins()了,这会更新fight对象以反映他赢了。

于 2013-07-26T05:34:39.567 回答
1

简单地增加计数器就足够了:

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] )

您需要检查“fight1!= 2”的功能“最佳”,现在应该是红色的“fight1.best!= 2”。您的程序可以读取:

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()

为了回答您的评论,我将提供整个战斗的示例实现:

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.') ]
于 2013-07-26T05:35:31.370 回答