login_success = False
def user_login():
login_username = input("Enter your username to login.\n")
login_password = input("Enter your password to login.\n")
credentials_checking = open("user_credentials.txt", "r")
while not login_success:
for line in credentials_checking:
credential_element = line.split(" | ")
if login_username == credential_element[0] and login_password == credential_element[1][:-1]:
print("Login successful!")
else:
login_success = True
break
credentials_checking.close()
login_success is a global variable but unfortunately an error occur. (This project do not allow the use of global function)The output are as below:
Output:
UnboundLocalError: local variable 'login_success' referenced before assignment
CodePudding user response:
Python functions treat all variables as they haven't been created yet, you just need to add global login_success
to the function.
def user_login():
global login_success
# all other code
CodePudding user response:
You need to use the keyword global
. Right now, Python thinks you're trying to refer to a local variable named login_success
. To fix this, add the following line to your function:
def user_login():
global login_success
...