0

以下是我的代码中可能与问题有关的部分:(我尽可能地删掉了)

import os
import getpass

def PAUSE():
    input("= Press <ENTER> to continue...")

def clearscreen():
    os.system('cls' if os.name=='nt' else 'clear')

def loginscreen():
    clearscreen()
    print("==================================================================")
    print("=                              LOGIN                             =")
    print("==================================================================")
    print("= None Important.                                                =")
    print("==================================================================")
    username = input("= Please enter your username: ")
    password = getpass.getpass("= Please enter the password that belongs to that username: ")
    print("==================================================================")
    try:
        # I had too cut away the MariaDB Section for a MCVE, and thus i had to fill the dbusername and sdbpassword and isadmin, but without modifying relevant code. Thus i might have made a mistake in this array, dont use them alot sooo... if this were to occur i am sorry....
        ['dbusername = "stackoverflow", dbpassword = "stackoverflow", isadmin = "No"']
        for row in results:
            dbusername = row[0]
            dbpassword = row[1]
            isadmin = row [2]
            if username == dbusername:
                if password == dbpassword:
                    if isadmin == "Yes":
                        admin_main_menu()
                    elif isadmin == "No":
                        clearscreen()
                        main_menu()
########## For some reason the same problem arises when i use the commented away code under this comment.
#        clearscreen()
#        print("==============================================")
#        print("=         Unkown Username / Password         =")
#        print("==============================================")
#        PAUSE()
#        print("==============================================")
#        loginscreen()
    except:
        clearscreen()
        print("Failed to check codes. (Error: 5646FCJU), Contact N.S. Geldorp")
        PAUSE()       

def main_menu():
    clearscreen()
    print("=============================================")
    print("=                 Main Menu                 =")
    print("=============================================")
    print("= 1. All unimportant...                     =")
    print("= 5. Exit                                   =")
    print("=============================================")
    answer = input("= Please enter the number of the function you wish to use: ")
    print("=============================================")
    clearscreen()
    if answer == "1":
#        print_animals()
        print("Not needed")
        PAUSE()
    elif answer == "5":
#        pass
        print("Exiting...")
        exit()
    else:
        print("Unimportant...")
        PAUSE()
        main_menu()

现在,除了登录屏幕和标准主菜单的相关部分之外,我删除了所有内容。当然还有 PAUSE 和 clearscreen 等功能,因为它们总是会重新出现在相关功能中。至少如果我写了它们。现在发生的情况是,当我成功登录并进入菜单,然后我决定退出时,它显示了登录屏幕之外的错误...我不明白,是吗?

4

1 回答 1

1

这是 1,442,633 为什么你永远不能使用空白except子句的示范。

sys.exit()通过引发异常来工作:SystemExit. 通常,该异常会一直冒泡到解释器,它会捕获它并优雅地退出。但是因为你的 try/except 代码捕获了一切,它也捕获了它;因此您会看到自己的错误消息,而不是解释器退出。

你应该只抓住你知道你可以处理的事情。我不确定您期望该代码出现什么异常,但大概它们是由数据库代码引发的异常。你应该计算出哪些可以被提升,并且只捕获那些:例如,except TypeError:.

至少,您应该限制您的 except 只捕获实际错误,您可以使用except Exception:; SystemExit 源自 BaseException,它是 Exception 的父类,所有其他运行时错误都源自它。但是,你真的不应该这样做,你应该只捕获特定的异常。

(另请注意,对数据库结果进行 for 循环是没有意义的;我不明白你为什么这样做。)

于 2018-11-26T09:11:57.790 回答