Python: 为什么我不能在 if 循环外调用方法

Python: Why can't I call a method outside the if loop

我是初学者,无法理解为什么我可以在 if 循环中调用此方法,但在外部却出现错误。

下面的代码来自 main.py 文件。它从另一个文件导入角色 class,从第三个文件导入房间 class。当我排除故障时,我发现一切正常,如下所示。但是当两个被注释掉的行被取消注释时,第二个未被注释的行 print(inhabitant.describe()) 出现错误,它说:

现在一切正常,我将删除下面的两行注释,但我的问题是为什么相同的代码行在 if 循环内有效,但不能立即在其外。

self.get_details() 方法工作正常 self.get_character() 方法工作正常 为什么 print(inhabitant.describe()) 在 if 循环内工作而不在循环外工作。 请忽略#print("\n ")

如有任何帮助,我们将不胜感激。

while True:
    print("\n")
    current_room.get_details()

    inhabitant = current_room.get_character()
   #print(inhabitant)
   #print(inhabitant.describe())

    if inhabitant is not None:
        #print("\n")
        print(inhabitant.describe())

下面是 character.py 文件中的代码。上面代码中提到的居民是一个subclass,Enemy.

 class Character():

    # Create a character
    def __init__(self, char_name, char_description, M_or_F):
        self.name = char_name
        self.description = char_description
        self.gender = M_or_F
        self.pronoun = None
        self.poss_pronoun = None
        self.conversation = None

    # Set up pronouns
    def set_pronoun(self):
        if self.gender == "m":
            self.pronoun = "He"
        else:
            self.pronoun = "She"
        return self.pronoun

    # Set up possessive pronouns
    def set_poss_pronoun(self):
        if self.gender == "m":
            self.pronoun = "His"
        else:
            self.pronoun = "Her"
        return self.pronoun

    # Describe this character
    def describe(self):
        return("\n%s is here!\n%s" %(self.name, self.description))

    # Set what this character will say when talked to
    def set_conversation(self, conversation):
        self.conversation = conversation

    # Talk to this character
    def talk(self):
        if self.conversation is not None:
            print("[" + self.name + " says]: " + self.conversation)
        else:
            print(self.name + " doesn't want to talk to you")

    # Fight with this character
    def fight(self, combat_item):
        print(self.name + " doesn't want to fight with you")
        return True

class Enemy(Character):
    def __init__(self, char_name, char_description, M_or_F):
        super().__init__(char_name, char_description, M_or_F)
        self.weakness = None
        self.possession = None

    def set_weakness(self, weak_point):
        self.weakness = weak_point

    def get_weakness(self):
        return self.weakness

    def set_possession(self, possession):
        self.possession = possession

    def get_possession(self):
        return self.possession

    def fight(self, combat_item):
        if combat_item == self.weakness:
            print("\nYou fend %s off with the %s" %(self.name, combat_item))
            return True
        else:
            print("\n%s crushes you, puny adventurer" %(self.name))
            return False

    def bribe(self, bribe_item):
        print("\nOK, I'll take your %s.\nHere is the %s you wanted." %(bribe_item, self.posession))
        self.possession = bribe_item 

    #def sleep(self, )

问题已解决。一个 AttributeError 被抛出是因为游戏在没有分配角色的厨房开始,因此它无法完成 print(inhabitant.describe())。在 IF 语句中,它首先检查房间是否包含字符,然后再尝试描述它。

感谢您的帮助。