类-属性错误

时间:2018-08-11 23:24:53

标签: python

我目前正在阅读《学习Python 3艰难的道路》一书。

我已经完成了练习43,其中写了一个小型文本冒险游戏来练习面向对象的编程。

我已经完成了程序代码的输入,并使其正常运行而没有任何错误。然后在其他研究部分,作者要求将简单的战斗系统添加到脚本中。

可以在此链接上看到完整的脚本,我不想将ex43的整个脚本粘贴到此问题中。

https://github.com/ubarredo/LearnPythonTheHardWay/blob/master/ex43.py

我创建了以下脚本作为战斗系统:

loadNextContainer.innerHTML = "<span class=\"sr_43\" id=\"srloadnext_2\" onclick=\"srt.loadNextMatchRecords('"+numDownloadSoFar+"', '"+maxDownloadLimit+"', '"+folderName+"', '"+jsonHashTags+"', '"+fromDate+"', '"+toDate+"', '"+lastScanId+"')\">LoadNext</span>";

这本身就很好用,我想在本书的脚本中使用它。

我试图从CentralCorridor类中调用它。玩家输入“拍摄”后,我通过添加以下内容调用了我编写的脚本:

import random
import time


class Player:

    def __init__(self, hit_points, sides):
        self.hit_points = hit_points
        self.sides = sides

    def take_hit(self):
        self.hit_points -= 2

    def roll(self):
        return random.randint(1, self.sides)


class Combat(Player):

    def combat_start():

        p = Player(hit_points = 10, sides = 6)
        e = Player(hit_points = 8, sides = 6)

        battle = 1
        while battle != 0:
            human = p.roll() + 6
            print("Your hit score: ", human)
            monster = e.roll() + 6
            print("Enemy hit score: ", monster)

            if human > monster:
                e.take_hit()
                print("Your hit points remaining: ", p.hit_points)
                print("Enemy hit points remaining:", e.hit_points)
                if e.hit_points == 0:
                    battle = 0
                    print("The Enemy is Dead!!!")
                time.sleep(2)

            elif human < monster:
                p.take_hit()
                print("Your hit points remaining: ", p.hit_points)
                print("Enemy points remaining: ", e.hit_points)
                if p.hit_points == 0:
                    battle = 0
                    print("You died in Battle!!!")
                time.sleep(2)

Combat.combat_start()  

我希望会发生的是我写的战斗课开始,然后当玩家获胜时它将继续到下一个场景,如果玩家输了它将会返回死亡课。

经过多次尝试和失败后,我将Combat类更改为:

Combat.combat_start()

添加了此脚本后,脚本便开始运行并退出循环。

class Combat(Scene, Player):

如果敌方玩家得分更高,则在一轮或两轮后循环中断。

我正在努力寻找解决方案,非常感谢能指出我要去哪里的问题。

通过其他答案,我发现有些评论认为这不是用Python编写OOP的理想方法。

在书中显示:

Your hit score:  12
Enemy hit score:  12
Your hit score:  10
Enemy hit score:  11
Your hit points remaining:  8
Enemy points remaining:  4
Your hit score:  10
Enemy hit score:  10
Your hit score:  12
Enemy hit score:  7
Your hit points remaining:  8
Enemy hit points remaining:  2
Your hit score:  7
Enemy hit score:  9
Your hit points remaining:  6
Enemy points remaining:  2
Your hit score:  7
Enemy hit score:  8
Your hit points remaining:  4
Enemy points remaining:  2
Your hit score:  9
Enemy hit score:  10
Your hit points remaining:  2
Enemy points remaining:  2
Your hit score:  10
Enemy hit score:  9
Your hit points remaining:  2
Enemy hit points remaining:  0
The Enemy is Dead!!!
Traceback (most recent call last):
  File "ex43.py", line 220, in <module>
    a_game.play()
  File "ex43.py", line 24, in play
    next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'

从我读到的内容来看,这是Python 2风格的OOP。我已经看到作者比Python 3更像是Python 2的粉丝。 我很喜欢他写在书中的练习,并希望继续进行下一个练习。

非常感谢您一如既往的帮助。

----更新----

这是所请求的Play方法:

class Scene(object):

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。写完之后,

1;71;72;15
2;76;77;20
3;71;72;25

我需要输入:

Combat.combat_start()

没有程序崩溃,因为它无处可去。

还修复了Combat类中的逻辑。

感谢您的帮助。

相关问题