Try-Except例外会创建更多例外 - python

我还在学习python,所以很幽默。我正在尝试进行文本冒险,将其作为一种学习的方式,并且正在尝试实现一种方式,使玩家能够响应提示(与嵌套互动一起返回字符串),并给出与之“互动”的事物列表。我正在尝试使程序打印“ self.location [“ interper”]“的值。

这是给我一些问题的代码。

# give the player the ability to "talk" and touch things

    # if there is nothing to interact with, say that there is nothing
    if "interobj" not in self.location and "interper" not in self.location:
        dismiss_fx.play()
        print("There's nothing of interest here.")

    # proceed if there is
    else:
        # print the things that are in the room
        confirm_fx.play()
        print("In the area, these things stand out to " + self.name + ": ")
        if "interobj" in self.location:
            print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + self.location["interobj"])
        if "interper" in self.location:
            print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + self.location["interper"])

        # prompt the user to interact with one of the things    
        interaction = input(colorama.Fore.CYAN + colorama.Style.BRIGHT + "\nWhich do you want " + self.name + " to interact with?: ")

        # determine if the thing the user interacted with is in the location
        try:
            raise KeyError(interaction)

        except KeyError as e:
            if str(e) != self.location["interobj"]:
                raise 

            elif interaction == self.location["interobj"]:
                # return whatever was noteworthy about the object
                confirm_fx.play()
                print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + self.location["intercom"])
                print("")

            checkprog(self,interaction)

        except KeyError as e:
            if str(e) != self.location["interper"]:
                raise

            elif interaction == self.location["interper"]:
                # return whatever the character had to say
                confirm_fx.play()
                print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + self.location["intersay"])
                print("")

            checkprog(self,interaction)

        except KeyError:
            # input is invalid
            invalid_fx.play()
            print(self.name + " couldn't find a '" + interaction + "'.")
            print("")

这应该做的是:

如果有一个名为“ interper”或“ interobj”的键,则提示用户输入。
解释输入并确定输入是“ interper”键还是“ interobj”键的值。
如果输入是这两个键之一的值,它将分别为“ interper”和“ interobj”输出键“ intersay”或“ intercom”的值。
如果这两个值都不是,则返回一条消息,指出输入无效。

实际上,它只是告诉我,我处理异常的方式是创建更多的异常。

TL; DR:有人可以以一种易于理解的方式解释异常处理吗?我不了解“筹款”功能。

如果可以帮助我,请提前谢谢。

参考方案

使用try:块时,正在检查该块中的代码是否引发异常(有错误)。如果有异常,解释器将立即跳转到except块并在那里执行代码。如果没有异常,则解释器仅执行try:块中的代码,然后移至except块之后的行。当您在try块中调用raise时,您就可以做到这一点,即使代码可以工作,也会引发“异常”,因为raise函数会自动引发异常。您只想将普通代码放在try块中而不加注,并且如果有异常,它将在except块中处理它。

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

Python:在不更改段落顺序的情况下在文件的每个段落中反向单词? - python

我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:text_in.txt具有两段,如下所示:Hello world, I am Here. I am eighteen years old. text_out.txt应该是这样的:Here. am I world, Hello old. years eighteen a…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…