Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何停止正在执行的函数中的代码?_Python_Login Control_Python 3.2 - Fatal编程技术网

在python中,如何停止正在执行的函数中的代码?

在python中,如何停止正在执行的函数中的代码?,python,login-control,python-3.2,Python,Login Control,Python 3.2,在Python3.2中,是否有方法停止函数的其余部分的执行 基本上,我正在创建一个登录系统作为我课程的一个概念,但我还没有在任何地方找到这个问题的答案 我的代码分为两个文件,一个日志记录程序,用日志文件处理输入和输出,以及主要类,如数据库连接、登录代码本身等 下面是处理获取用户输入的代码,我对第3行和第4行感兴趣,这两行将'quit'转换为'QUIT0x0',以尽量减少意外调用quit代码的可能性 def getInput(input_string, type): result = in

在Python3.2中,是否有方法停止函数的其余部分的执行

基本上,我正在创建一个登录系统作为我课程的一个概念,但我还没有在任何地方找到这个问题的答案

我的代码分为两个文件,一个日志记录程序,用日志文件处理输入和输出,以及主要类,如数据库连接、登录代码本身等

下面是处理获取用户输入的代码,我对第3行和第4行感兴趣,这两行将'quit'转换为'QUIT0x0',以尽量减少意外调用quit代码的可能性

def getInput(input_string, type):
    result = input(input_string)
    if result.lower == 'quit':
            result = 'QUIT0x0'
    #log the input string and result
    if type == 1:
            with open(logFile, 'a') as log_file:
                    log_file.write('[Input] %s \n[Result] %s\n' %(input_string, result))
                    return result
    #no logging
    elif type == 2:
            return result
    #undefined type, returns 'Undefined input type' for substring searches, and makes a log entry
    else:
            result = '[Undefined input type] %s' %(input_string)
            output(result, 4)
            return result
这是处理从用户数据库中删除用户记录的代码,我感兴趣的是如何使第4行和第5行工作,并停止执行函数的其余部分:

def deleteUser(self):
self.__user = getInput('Enter the username you want to delete records for: ', 1)
if self.__user == 'QUIT0x0':
    #Quit code goes here
else:
    self.__userList = []
    self.__curs.execute('SELECT id FROM users WHERE username="%s"' %(self.__user))
提前感谢,, Tom

调用“退出函数”返回:

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    return
  else:
    # ...
但是由于您已经使用了
if/else
,因此无论如何都不应该执行
else
分支,因此返回是不必要的。你也可以在那里放一个
通行证

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    pass
  else:
    # ...
甚至可以使用以下选项:

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user != 'QUIT0x0':
    # ...
甚至使用提前归还:

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    return
  # ...
“退出函数”被称为
返回

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    return
  else:
    # ...
但是由于您已经使用了
if/else
,因此无论如何都不应该执行
else
分支,因此返回是不必要的。你也可以在那里放一个
通行证

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    pass
  else:
    # ...
甚至可以使用以下选项:

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user != 'QUIT0x0':
    # ...
甚至使用提前归还:

def deleteUser(self):
  self.__user = getInput('Enter the username you want to delete records for: ', 1)
  if self.__user == 'QUIT0x0':
    return
  # ...

@比利:像“我不明白”或“我应该自己弄明白”那样愚蠢另外,如果有帮助的话,请考虑用左边的嘀嗒按钮来回答这个问题。“我应该自己解决这个问题,我会的,但是它不会让我再去六。”minutes@Billie:像“我不明白”或“我应该自己弄明白”那样愚蠢另外,如果有帮助的话,请考虑使用左边的嘀嗒按钮来回答这个问题。“我应该自己解决这个问题,我会的,但是它不会让我再过六分钟<代码>结果。更低的< /代码>应该是代码>结果。
代码是意外调用的?
result.lower
应该是
result.lower()
为什么会意外调用
quit()
代码?