当我告诉它退出时,我的程序没有停止

时间:2019-07-16 09:11:46

标签: python python-3.x

当我按5退出时,它会出现错误,但不起作用

由于某种原因

import sys
sys.exit()

不起作用

def ask (user) :

    if user == 1:
        user = input('select a file with an ASCII art image ')
        f = open(user, 'r')
        if f.mode == 'r':
            showart = f.read()
            print(showart)
            print("You will be returned to the menu")

    elif user == 2:
        print('hi')

    elif user == 3:
              print ('hi')

    elif user == 4:
            print("hi")

    elif user == 5:
            print('goodbye')
            import sys
            sys.exit()


while (True) :
    print("1 - Display ASCII art ")
    print("4 - convert RLE option ")
    print("5 - Quit ")
    print("   ")
    print("   ")
    print("   ")

    try:
        user=int(input("Select an integer between 1 and 5 : "))

        if (user<5 and user > 1) :
            ask(user)
        else:
                user=int(input("Please enter a number between 1 and 5 : "))
                while (user > 5 or user < 1) :
                    user=int(input("Please enter a number between 1 and 5 : "))

                ask (user)
    except:
        print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')

它会打印再见,但不会退出程序

3 个答案:

答案 0 :(得分:2)

由于sys.exit()引发错误,该错误被except捕获并由于while True而再次运行

作为评论者中的提及者,您可以简单地添加Exception类型,因为SystemExit不是Exception的子类型,它将不会被捕获,然后可以退出正确编程

def ask(user):
    if user == 1:
        user = input('select a file with an ASCII art image ')
        f = open(user, 'r')
        if f.mode == 'r':
            showart = f.read()
            print(showart)
            print("You will be returned to the menu")

    elif user == 2:
        print('hi')
        return 0

    elif user == 3:
        print('hi')

    elif user == 4:
        print("hi")


while True:
    print("1 - Display ASCII art ")
    print("4 - convert RLE option ")
    print("5 - Quit ")
    print("   ")
    print("   ")
    print("   ")

    try:
        user = int(input("Select an integer between 1 and 5 : "))

        """
        If user enter value which is 5 then print 'goodbye'
        as you expect and break the while loop. So it's no
        longer running.
        """
        if user == 5:
            print('goodbye')
            break

        """
        5 > user > 0 mean match only 4, 3, 2, 1

        this loop will continue when the user entered an
        integer value which is not belongs to 4, 3, 2, 1.
        Otherwise it goes to else statement and execute
        ask(user) function.
        """
        if not 5 > user > 0:  # [..., -1, 0, 5, 6, ...]
            continue
        else:
            ask(user)
    except Exception:
        print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')

答案 1 :(得分:0)

您正在捕捉sys.exit

  

从Python退出。这是通过引发SystemExit异常来实现的,因此可以使用try语句的finally子句指定的清除操作,并且有可能在外部级别拦截出口尝试。

https://docs.python.org/3/library/sys.html#sys.exit

答案 2 :(得分:0)

我只是更改您的代码,但我不知道它是否符合您的要求,并在代码注释中解释了我所做的事情。

def ask(user):
    if user == 1:
        user = input('select a file with an ASCII art image ')
        f = open(user, 'r')
        if f.mode == 'r':
            showart = f.read()
            print(showart)
            print("You will be returned to the menu")

    elif user == 2:
        print('hi')
        return 0

    elif user == 3:
        print('hi')

    elif user == 4:
        print("hi")


while True:
    print("1 - Display ASCII art ")
    print("4 - convert RLE option ")
    print("5 - Quit ")
    print("   ")
    print("   ")
    print("   ")

    try:
        user = int(input("Select an integer between 1 and 5 : "))

        """
        If user enter value which is 5 then print 'goodbye'
        as you expect and break the while loop. So it's no
        longer running.
        """
        if user == 5:
            print('goodbye')
            break

        """
        5 > user > 0 mean match only 4, 3, 2, 1

        this loop will continue when the user entered an
        integer value which is not belongs to 4, 3, 2, 1.
        Otherwise it goes to else statement and execute
        ask(user) function.
        """
        if not 5 > user > 0:  # [..., -1, 0, 5, 6, ...]
            continue
        else:
            ask(user)
    except:
        print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')

如果您一无所获,请对此答案发表评论,我会为您提供帮助。