Python3编译错误

时间:2016-12-03 05:39:01

标签: python-3.x

错误讯息:

FE..
************ MISSING LINE ***************
file 1 line number: 2
missing line in file 2, no match for file 1 line:
'=================== ROUND 1 ===================\n'
*****************************************

F..
======================================================================
ERROR: test_combat_round (__main__.TestRPG)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_rpg.py", line 64, in test_combat_round
    self.assertIsNone(combat_round(self.rich, self.thompson))
  File "/Users/ajsmitty12/Desktop/ISTA130/rpg.py", line 124, in combat_round
    player1.attack(player1, player2)
AttributeError: 'int' object has no attribute 'attack'

======================================================================
FAIL: test_attack (__main__.TestRPG)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_rpg.py", line 42, in test_attack
    self.assertEqual(out, buf.getvalue())
AssertionError: 'Rich attacks Thompson!\n\tHits for 5 hit points!\n\tThompson         h[24 chars]g.\n' != 'Rich attacks Thompson (HP: 10)!\n\tHits for 5 hit     points!\n\tT[33 chars]g.\n'
- Rich attacks Thompson!
+ Rich attacks Thompson (HP: 10)!
?                      +++++++++
    Hits for 5 hit points!
    Thompson has 5 hit points remaining.


======================================================================
FAIL: test_main (__main__.TestRPG)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_rpg.py", line 117, in test_main
    self.assertTrue(compare_files('rpg_main_out_correct.txt', out))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 7 tests in 0.006s

FAILED (failures=2, errors=1)
Correctness score =  57.14285714285714%



    Description:
    -This program will simulate combate for a simple Role Playing Game (RPG).
    - The game has a single type of character, the Fighter.
    -Each inidividual Fighter has an integer value caled "hit points" that represents his/her current health (the amount of damage he/she can sustain before death)
    -It will simulate combat between 2 Fighers. 
    -Combat is divided into rounds:
    -During each round each combatant will make 1 attempt to stike the other. 
    -We will use random numbers to simulate rolling dice, the higher number attacks first, and the lower number goes 2nd if still alive. 
    -If there is a tie, the players will go at the same time, they will attack eachother at the same moment. 
    -During a simultaneous attack both Fighters will get to attack even if one (or both) if (are) killed during that round. 
    -The progam will use a random number to determine whether an attack attempt is successful or not. 
    -Each successful attack will inflict damage on the opponent. 
    -To simulate damage, we'll reduce the opponent's hit points by another random number
    -When a Fighters hit points are reduced to 0 (or less than 0) that player is considered to be dead. 
    -Combat round continue until one(or both) combatants are dead. 
    '''

我很难弄清楚如何调用我在课堂上创建的战斗机实例,这也是我的战斗功能,这是在课外。 我也想知道我是否正确比较了player1和player2的随机数。当搞乱它时,我尝试使用:     player1 =战斗机()     player2 =战斗机() 这样做我的错误消息会说Fighter()>战斗机()无法比拟。 最后,不太确定为什么我的test_attack失败了。

我的代码(到目前为止):

import random
class Fighter:
    def __init__(self, name):
      '''
     -This is my initializer method, which takes 2 parameters: 
            -Self
            -Name (a string, the name of a fighter)
        -This method will set a name attribute to the value of the name parameter
        -It will also set a hit_points attribute to 10 (all fighters begin life with 10 hit points).
        '''
        self.name = name
        self.hit_points = 10

    def __repr__(self):
        '''
        -This method takes 1 parameter, self. 
        -It returns a string showing the name and hit points of the instances in the following format:
                Bonzo (HP:  9)
        '''
        result = str(self.name) + " (HP: " + str(self.hit_points) + ")"
        return result

    def take_damage(self, damage_amount):
        '''
        -This method takes 2 parameters:
            -self (the Fighter instance that calls the method)
            -damage_amount (an integer representing the number of hit points of damage that have just been inflicted on this Fighter):
                -The method should first decrease the hit_points attribute by the damage_amount.
                -Next, it should check to see if the Fighter has died from the damage:
                    -A hit_points value that is 0 or less indicates death, and will print a message like the following:
                        \tAlas, Bonzo has fallen!
                    -Otherwise, it will print a message like the following (in this example, the player had 5 hit points left over after damage)
                        \tBonzo has 5 hit points remaining. 
                -The method returns nothing.

        ''' 
        self.hit_points = self.hit_points - damage_amount
        if self.hit_points <= 0:
            print('\t' + 'Alas, ' + str(self.name) + ' has fallen!')
        else:
            print('\t' + str(self.name) + ' has ' + str(self.hit_points) + ' hit points remaining.')
    def attack(self, other):
        '''
        -This method takes 2 parameters:
            -self 
            -other (another Fighter instance being attacked by self)
        -The method will print the name of the attacker and attacked in the following format:
            Bonzo attacks Chubs!
        -Next, determine whether the attack hits by generating a random integer between 1 and 20
            -Use the randrange function from the random module to get this number.
            -A number that is 12 or higher indicates a hit:
                - For an attack that hits, generate a random number between 1 and 6(using random.randrange) to represent the amount of damage inflicted by the attack 
                    ***Do NOT use the from random import randrange sytanze (randrange works like range, not like randint (think about the upper bounds you choose)).
                - Print the amount of damage inflicted like the following:
                    \tHits for 4 hit points!
                -Invoke the take_damage method on the victim (i.e. on other), passing it the amount of damage inflicted. 
            -For an attack that misses, print the following message:
                \tMisses!
        -This method returns nothing. 
        '''
        self.other = Fighter(self.name)
        print(str(self.name) + ' attacks ' + str(other) + '!')
        attack = random.randrange(1, 20)
        if attack >= 12:
            damage_amount = random.randrange(1, 6)
            print('\t' + 'Hits for ' + str(damage_amount) + ' hit points!')
            other.take_damage(damage_amount)
        else:
            print('\t' + 'Misses!')

    def is_alive(self):
        '''
        -This method takes 1 parameter, self. 
            -It returns True if self has a positive number of points, False otherwise.
        '''

        if self.hit_points > 0:
            return True
        else:
            return False
def combat_round(player1, player2):
    '''
    -This function takes 2 parameters:
        -The 1st is an instance of Fighter. 
        -The 2nd is another instance of Fighter. 
    -It determines which of the 2 fighters attacks 1st for the round by generating a random interger (use randrange) between 1 and 6 for each fighter:
        -if the numbers are equal:
            -print the following message:
                Simultaneous!
            -Have each figher instance call his attack method on the other figher (the 1st figher in the argument list must attack first to make the test work correctly)
        -if one number is larger:
            -the fighter with the larger roll attacks 1st (by calling his attack method and passing it the figher being attacked).
            -if the 2nd fighter survives the attack (call is_alive), he then   attack the 1st. 
    -This function returns nothing.
    '''
    Fighter.name = player1
    Fighter.name = player2
    player1 = random.randrange(1, 6)
    player2 = random.randrange(1, 6)
    if player1 == player2:
        print('Simultaneous!')
        player1.attack(player2)
    if player1 > player2:
        player1.attack(player2)
        if player2.is_alive() == True:
            player2.attack(player1)
    if player2 > player1:
        player2.attack(player1)
        if player1.is_alive() == True:
            player1.attack(player2)

1 个答案:

答案 0 :(得分:1)

你上课看起来没问题,但combat_round函数有一些问题。

您使用player1 = Fighter()等进入了正确的轨道。

player1 = Fighter(name = 'Jeff')
player2 = Fighter(name = 'Bill')

但是现在你想先选择一个。您无法将它们与&gt;,&lt;,= =进行比较,而无需自定义__eq__()等特殊方法,因为Python不知道要比较哪个属性。如何设置属性然后比较它:

player1.speed = random.randrange(1,6)
player2.speed = random.randrange(1,6)

现在你可以编写这样的测试:

if player1.speed == player2.speed:
    print("Simultaneous!")

此外,如果函数combat_round()使用战斗机的实例,那么您不需要在函数内实例化它们。但是,如果该函数接受了作为战士名字的字符串,那么你就会像我的例子一样。