【问题标题】: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