有没有办法根据条件退出当前功能但不结束程序
Is there a way to quit the current function based on a condition but not end the program
我有 2 个 class 方法。一种方法 'accountExistance()' 通过在数据库中查找重复的用户名来检查帐户是否已存在,另一种方法 'customerDetails()' 将用户注册详细信息写入数据库。我的 customerDetails() 方法调用 'accountExistance() 方法,如果在 'accountExistance' 方法中检查了重复的用户名,我 return False 并返回初始方法,如果 return 值为 False 'sys.exit()' 发生。
问题在于我宁愿不退出程序,因为它是 运行 一个 tkinter gui,允许用户更改注册详细信息。相反,有没有一种方法可以在不退出的情况下取消执行,而不是将重复的值提交到数据库。
请注意,在用户按下注册时从另一个文件调用 customerDetails(),其中来自输入框的 stringVars() 被输入 class 方法。此外,此处的缩进外观格式不正确,但我制作了一个最小的逻辑示例,并删除了一些代码行。
class Database():
def __init__(self):
Database.phoneNumber = ""
def accountExistance(email, phoneNumber):
conn=sqlite3.connect("system.db")
cur=conn.cursor()
emailExist = cur.execute("SELECT count(email) FROM customerDetails WHERE email = ?", (email,)).fetchall()
conn.commit()
print(emailExist)
phoneExist = cur.execute("SELECT count(phone) FROM customerDetails WHERE phone = ?", (phoneNumber,)).fetchall()
print(phoneExist)
conn.commit()
if emailExist or phoneExist != 0 :
tk.messagebox.showinfo("Error", "This email and/or phone number is associated with an account")
return False
sys.exit()
#else:
#return True
#INSERT SIGN UP DETAILS
def customerDetails(forename, surname, dob, address, city,
county, postcode, phone, email, password,verified, gender):
print(forename, surname, dob, address, city, county, postcode,
postcode, phone, email, password, verified, gender)
test = Database.accountExistance(email, phone)
if test == False:
sys.exit()
age = 0
conn=sqlite3.connect("system.db")
cur=conn.cursor()
cur.execute("INSERT INTO customerDetails VALUES
(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)",(forename, surname, dob, address, city,
county, postcode, phone, email, password, verified, age, gender))
conn.commit()
conn.close()
只需将您的条件更改为以下内容:
test = Database.accountExistance(email, phone)
if test == False:
return # Stops the execution of the function
此外,如果test
必然是True
或False
,您可以直接查看其值:
if not test:
与
相同
if test == False:
我有 2 个 class 方法。一种方法 'accountExistance()' 通过在数据库中查找重复的用户名来检查帐户是否已存在,另一种方法 'customerDetails()' 将用户注册详细信息写入数据库。我的 customerDetails() 方法调用 'accountExistance() 方法,如果在 'accountExistance' 方法中检查了重复的用户名,我 return False 并返回初始方法,如果 return 值为 False 'sys.exit()' 发生。
问题在于我宁愿不退出程序,因为它是 运行 一个 tkinter gui,允许用户更改注册详细信息。相反,有没有一种方法可以在不退出的情况下取消执行,而不是将重复的值提交到数据库。
请注意,在用户按下注册时从另一个文件调用 customerDetails(),其中来自输入框的 stringVars() 被输入 class 方法。此外,此处的缩进外观格式不正确,但我制作了一个最小的逻辑示例,并删除了一些代码行。
class Database():
def __init__(self):
Database.phoneNumber = ""
def accountExistance(email, phoneNumber):
conn=sqlite3.connect("system.db")
cur=conn.cursor()
emailExist = cur.execute("SELECT count(email) FROM customerDetails WHERE email = ?", (email,)).fetchall()
conn.commit()
print(emailExist)
phoneExist = cur.execute("SELECT count(phone) FROM customerDetails WHERE phone = ?", (phoneNumber,)).fetchall()
print(phoneExist)
conn.commit()
if emailExist or phoneExist != 0 :
tk.messagebox.showinfo("Error", "This email and/or phone number is associated with an account")
return False
sys.exit()
#else:
#return True
#INSERT SIGN UP DETAILS
def customerDetails(forename, surname, dob, address, city,
county, postcode, phone, email, password,verified, gender):
print(forename, surname, dob, address, city, county, postcode,
postcode, phone, email, password, verified, gender)
test = Database.accountExistance(email, phone)
if test == False:
sys.exit()
age = 0
conn=sqlite3.connect("system.db")
cur=conn.cursor()
cur.execute("INSERT INTO customerDetails VALUES
(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)",(forename, surname, dob, address, city,
county, postcode, phone, email, password, verified, age, gender))
conn.commit()
conn.close()
只需将您的条件更改为以下内容:
test = Database.accountExistance(email, phone)
if test == False:
return # Stops the execution of the function
此外,如果test
必然是True
或False
,您可以直接查看其值:
if not test:
与
相同if test == False: