在 python 3.2 中,有没有办法停止函数的其余部分执行?
基本上,我正在创建一个登录系统作为我的课程的概念,但我无法在任何地方找到答案。
我的代码分为 2 个文件,一个记录器,它使用日志文件处理输入和输出,以及主要的类,例如数据库连接、登录代码本身等。
这是处理获取用户输入的代码,我对第 3 行和第 4 行感兴趣,它们将“quit”转换为“QUIT0x0”,以尽量减少意外调用退出代码的机会。
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))
在此先感谢,汤姆