【问题标题】:Questions about python inheritance and argument lists关于python继承和参数列表的问题
【发布时间】:2023-04-06 21:22:01
【问题描述】:

首先我得到了这个错误

File "E:\New folder (7)\maingame.py", line 64, in play     print self.introduction AttributeError: 'game' object has no attribute 'introduction'

我不确定这意味着什么,因为我正在从上一课中提取 self.introduction..

我也收到了

File "E:\New folder (7)\maingame.py", line 96, in <module>
game.play()
TypeError: play() takes exactly 2 arguments (1 given)

错误,但我终生无法找到它正在寻找的参数,我只是希望它能够工作。

  from random import random

  class place(object):
    def __init__(self, title, description, events):
    self.title = title
    self.description = description
    self.events = events

class event(object):
def __init__(self, probability, message, healthChange):
    self.probability = probability
    self.message = message
    self.healthChange = healthChange

def process(self):
    if random() < self.probability:
        print self.message
        return self.healthChange
    return 0

class textadventure():
def __init__(self):
    super(textadventure, self).__init__()
    self.introduction = """ 
Welcome player, you are a lone traveler in space whom has set out to find glories    beyond measure. 
Unfortunately for you the dread pirate Roberts has attacked. You must defeat him.
    """
    commandDeck = place('Command Deck', "You are now in the command center, here you can drive the ship and fire its weapons.",(
        event(0.7, "The pirate ship fires at you! You take damage to your engines!", -10),
        event(0.2, "One of the pirates manages to beam onto your ship! He shoots you before beaming away!",0),
        ))

    engineRoom = place('Engine Room', "You are now in the main engine room here you can repair damage to the ship",(
        event(0.7, "The pirate ship fires at you! You take damage to your engines!", -10),
        ))

    restQuarters = place('Resting Quarters', "Here you can take a rest and heal your self",(
        event(1.0, 'You are able to patch up your wounds and get back to the battle',0),
        event(0.5, "The pirate ship fires at you! You take damage to your engines!", -10),
        ))

    commandDeck.transitions = (engineRoom, restQuarters),
    engineRoom.transitions = (commandDeck, restQuarters),
    restQuarters.transitions = (commandDeck, engineRoom),

    self.location = commandDeck
pirateHp = 50
class game(object, textadventure):
    def __init__(self):
            super(game, self).__init__()
            self.health = 100
    def location(self):
            if self.location == commandDeck:
                    choice = raw_input('would you like to fire on the enemy ship?')
                    if choice == 'yes':
                            print 'You have hit the pirates!'
                            pirateHp -= 10
                    else: choice == 'no'
            elif self.location == engineRoom:
                    choice = raw_input('Would you like to repair the engines?')
                    if choice == "yes":
                            event(1, "You repair what you can of the engines.", 10)
    def __init__(self):
            self.health = 100
    def play(self, textadventure):
            print textadventure.introduction 

            while True:
                    print (self.location.description)
                    for event in self.location.events:
                            self.health += event.process()
                            if self.health <= 0:
                                    print ("Your ship has been destroyed!")
                                    pause
                                    exit(1)
                    print ('Your ships health is at %d percent' % self.health)
                    self._transition()

    def _transition(self):
            transitions = self.location.transitions
            print ('you can go to: ')
            for (index, transition) in enumerate(transitions):
                    print (index + 1, transition.title)

            choice = int(raw_input('Choose one '))
            if choice == 0:
                    exit(0)
            else:
                    self.location = transitions[choice - 1]

    def pirateShip(Object): 
            if pirateHp == 0:
                    print "You have defeated the pirates! Congradualations!"
                    pause
                    exit(1)

game = game()
game.play(game)

【问题讨论】:

  • 您至少必须在问题中正确格式化它。并且不要忘记放 full 回溯。
  • 我重新编辑了,谢谢。不讽刺真正的感谢
  • 你真的应该把这个问题当作两个单独的问题来问,因为(很可能)很明显这里有两个完全不同的问题。

标签: python-2.7 typeerror attributeerror


【解决方案1】:

“游戏”对象没有“介绍”属性

你应该在初始化游戏时调用你的超类的init。在您当前的代码中, textadventure.init 从未被调用,这就是为什么不会将介绍添加到 textadventure 的原因。

游戏也不应该从对象继承(它通过 textadventure 实现)。

class game(textadventure):
    def __init__(self):
            super(game, self).__init__()
            self.health = 100

    def play(self):
        print self.introduction

应该做的伎俩。


TypeError: play() 只需要 2 个参数(给定 1 个)

你永远不会在游戏中使用你的textadventure 参数。删除它应该可以正常工作。

【讨论】:

  • 好吧等等我有点迷糊,当我调用上一个类的时候,是不是也需要调用上一个类到类中的方法?
  • 我不太明白你想说什么。
  • 这里也需要调用textadventure的init吗? def play(self):
  • 我更改了上面的代码以反映我现在所拥有的。如何更改播放方法以使其在 textadventure 类中调用?--- 如果需要
  • 没有。您只需要在 init 中调用它,否则 self.introduction 将永远不会被分配。我编辑了我的答案以包含 play 方法的可能实现。
猜你喜欢
  • 2020-03-09
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多