【问题标题】:Python: Why can't I call a method outside the if loop [closed]Python:为什么我不能在 if 循环之外调用方法[关闭]
【发布时间】:2021-08-31 03:23:11
【问题描述】:

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

以下代码来自 main.py 文件。它从另一个文件导入一个字符类,从第三个文件导入一个房间类。当我进行故障排除时,我发现一切正常,如下所示。但是当两个注释掉的行被取消注释时,第二个未注释的行 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 文件中的代码。上面代码中提到的居民是一个子类,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, )

【问题讨论】:

  • 请告诉我们current_room是什么。
  • current_room.get_character()方法中,我认为你需要有一个return语句,这样它就不是None Type
  • @U12-Forward current_room 是食堂。该代码没有问题。我确实犯了一个错误,问为什么它不在 WHILE 循环之外打印,而我的意思是 IF 循环。所有事情都发生在 while 循环中。我已经编辑了我的帖子。
  • @Japs6901 请在您的current_room 类中编辑问题
  • @Japs6901 我们需要看current_room 类...请给我们看

标签: python methods do-while


【解决方案1】:

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

感谢您的所有帮助。

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多