我正在创建一个小银行系统。现在,它完全在终端中。
您可以通过提供用户名和密码登录。我创建了一个类来验证给定的用户名和密码是否与数据库中的数据匹配。
如果不匹配,它将反复询问您的用户名和密码。
之后,将根据User类创建一个用户,依此类推...
问题是:
初次尝试输入凭据时,一切正常。
但是,当您输入错误的凭据时,应用程序将再次提示输入用户名和密码,而login()函数将返回用户名,但不会将其分配给用于创建用户的变量。
login()函数:
from Login.loginValidator import loginValidator
def login():
validator = loginValidator()
username = input("Enter Username: ")
password = input("Enter Password: ")
result = validator.validateLogin(username, password)
if result:
print(username)
return username
else:
login()
loginValidator类:
class loginValidator:
def validateLogin(self, username, password):
def checkIfUsernameExists(username):
connection = psycopg2.connect(
"dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT username FROM clientstandardinfo WHERE username = %s", [username])
result = cursor.fetchone()
cursor.close()
connection.close()
gc.collect()
if result is not None:
return True
result = checkIfUsernameExists(username)
if result:
connection = psycopg2.connect(
"dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT password FROM clientstandardinfo WHERE username = %s", [username])
passResult = cursor.fetchone()[0]
if sha256_crypt.verify(password, passResult):
print("Login Success")
return True
else:
print("Wrong password")
return False
else:
print("Username doesn't exist")
return False
getUserInfo函数:
def getUserInfo(clientUsername):
print("username from getUserInfo: \n", clientUsername)
connection = psycopg2.connect("dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT * FROM clientstandardinfo WHERE username = %s",[clientUsername])
result = cursor.fetchone()
print("Result from getUserInfo: \n",result)
cursor.close()
connection.close()
#1=firstname, 2=lastname, 3=username, 4=password,
# 5=email, 6=dateOfBirth, 7=gender, 8=admin, 9=dateofregister
return result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9]
main()函数:
def main():
username = login()
print("MAIN FUNCTION - username:\n",username)
client_user = User(*getUserInfo(username))
输出为(第一次输入错误的凭据后):
<prints correct username that comes from print in login() function>
MAIN FUNCTION - username:
None
username from getUserInfo:
None
Result from getUserInfo:
None
Traceback (most recent call last):
File "/Users/mac/Dropbox/banking raw/main.py", line 24, in <module>
main()
File "/Users/mac/Dropbox/banking raw/main.py", line 16, in main
client_user = User(*getUserInfo(username))
File "/Users/mac/Dropbox/banking raw/db.py", line 22, in getUserInfo
return result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9]
TypeError: 'NoneType' object is not subscriptable
Process finished with exit code 1
我不太明白为什么它不起作用。希望你能理解我的解释
感谢您的帮助。另外,欢迎您提供有关代码“质量”的任何反馈。
答案 0 :(得分:3)
您忘记了return
函数上的login
:
def login():
...
else:
return login()