您好,我在编程方面还很陌生。我一直试图第一次使用sqlite3进行此登录和注册程序。如您所见,我编写check_username函数的方式不正确,因为在循环外部使用了Break。您能否以一种不会产生语法错误的方式帮助我重新排列代码。任何建议都将非常有帮助。
import re
import sqlite3
specialChar = ['$', '#', '@', '!', '*']
connection = sqlite3.connect("login_database.db")
cursor = connection.cursor()
def creat_table():
cursor.execute("""CREATE TABLE IF NOT EXISTS Register
(username TEXT, password TEXT)""")
connection.commit()
def insert_info(userN, passW):
cursor.execute("INSERT INTO Register VALUES(?,?)", (userN, passW))
connection.commit()
def check_username_taken(userN):
while True:
userN = input("create a username: ")
cursor.execute("SELECT * FROM Register WHERE username=?", (userN,))
fetch = cursor.fetchone()
connection.commit()
if fetch is not None:
print("username taken")
else:
break
def check_username_exit(userN):
cursor.execute("SELECT * FROM Register WHERE username=?", (userN,))
fetch = cursor.fetchone()
connection.commit()
if fetch is not None:
break
else:
print("username is not registered.")
print("1.Register \n2.Login \n3.exit")
while True:
decision = input("What would you like to do? ")
if decision == "1":
while True:
userN = input("create a username: ")
check_username_taken()
while True:
passW = input("creat a password: ")
if len(passW) < 6:
print("Your password should at least contain 6 charcters.")
elif re.search("[0-9]", passW) is None:
print("Your password must contain at least one number.")
elif re.search("[a-z]", passW) is None:
print("Your password must contain at least one letter.")
elif not any(c in specialChar for c in passW):
print("Your password must contain at least one spcial character.")
else:enter code here
insert_info()
print("Password is set!")
break
if decision == "2":
while True:
userN = input("Username: ")
check_username_exit()
x = 0
while x !=1 :
passW = input("Password: ")
for key,val in list.items():
if key == userN and val == passW:
print("logging in ...")
x = 1
break
else:
print("wrong combination.")
break
if decision == "3":
print("\nEnd of the program!")
exit()