方法下的无限循环“try and if”

  1. 执行menu()并转到 1、2 或 3 就可以很好地完成其工作。

  2. 但是经过之后getproduct(character)再返回,menu()如果你选择3,就会形成一个糟糕的循环。

我想知道为什么,以及如何解决这个问题......

def menu():


    menu = '1. ice\n2. cream\n3. quit'

    print(menu)

    

    try:

        order = int(input('choose one: '))

        

        if order == 1:

            c = 'ice'

            getproduct(c)

        elif order == 2:

            c = 'cream'

            getproduct(c)

            

        elif order == 3:

            exit()

            

        else: menu()

        

    except ValueError: menu()



def getproduct(character):


    toping = int(input('1. ice or 2. cream?'))

    

    try:

        if character == 'ice' and toping == 1:

            print(character + 'ice')

            menu()

        

        elif character == 'ice' and toping == 2:

            print(character + 'cream')

            menu()

        elif character == 'cream' and toping == 1:

            print(character + 'ice')

            menu()

        elif character == 'cream' and toping == 2:

            print(character + 'cream')

            menu()

        else: getproduct(character)

    except: getproduct(character)

    

        

menu()


忽然笑
浏览 104回答 2
2回答

泛舟湖上清波郎朗

代码中有几个错误。首先,您使用的是 exit,不应在文件中使用它,相反,我在示例中使用具有相同目标的模块 sys (sys.exit(0))。另一方面,您以不精确的方式使用输入检查,并且循环应该有所不同。在菜单中,我个人推荐使用 while 循环。您的问题的解决方案和一些改进(可能更好):import sysdef menu():    menu = '1. ice\n2. cream\n3. quit'    while True:        print(menu)        try:            order = int(input('choose one: '))        except:            print("Use a correct answer")        else:            if order == 1:                c = 'ice'                getproduct(c)            elif order == 2:                c = 'cream'                getproduct(c)            elif order == 3:                sys.exit(0)            else:                print("Use a correct answer")def getproduct(character):    topings = '1. ice or 2. cream?: '    while True:        print(topings)        try:            second_order = int(input())        except:            print("Use a correct answer")        else:            if character == 'ice' and second_order == 1:                print(character + 'ice')                break            elif character == 'ice' and second_order == 2:                print(character + 'cream')                break            elif character == 'cream' and second_order == 1:                print(character + 'ice')                break            elif character == 'cream' and second_order == 2:                print(character + 'cream')                break            else:                print("Use a correct answer.")menu()

心有法竹

该exit()函数通过引发异常类型来工作SystemExit,异常类型沿着链向上传播,直到没有更多的东西可以运行并且程序停止。这是一个例外,意味着上面引发的代码SystemExit允许关闭资源并执行其他结束活动,以免破坏任何外部资源。然而,这也意味着空白except:语句可以捕获SystemExit并忽略它。这就是这里发生的事情。def getproduct(character):    toping = int(input('1. ice or 2. cream?'))        try:        ...        # call to menu(), which calls exit()    except: # this activates on ANY exception, including SystemExit        getproduct(character)一般来说,你几乎不应该使用原始except块,因为在这种情况下它会捕获你不希望它捕获的东西。相反,分析 中的代码try,找出它将抛出什么类型的异常,并捕获这些异常。在你的情况下,它可能是ValueError或者TypeError:try:    ...except (ValueError, TypeError):    getproduct(character)或者,如果您决心捕获所有内容,则可以为错误是 a 编写一个特殊的异常SystemExit(尽管,空白except:或 aexcept Exception:被认为是不好的做法):try:    ...except SystemExit:    pass  # ignore itexcept:    getproduct(character)根据文档,您应该注意:quit(code=None) exit(code=None) 在打印时,打印一条消息,如“使用 quit() 或 Ctrl-D(即 EOF)退出”,并在调用时,使用指定的退出代码引发 SystemExit。您可能应该使用sys.exit()它,尽管它的作用基本相同。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python