I'm very new to Python and was testing myself on making a login and signup account database type thing (very basic) it's all text based.
Some reason, when I run the code and signup, it needs the accounts dictionary inside of signup(), but when I successfully create the account, it doesn't store the new account key and information in the accounts dictionary in def login() (hence why I put accounts dictionary in signup() too, because otherwise I get a error for accounts dictionary not existing) please help?
https://i.sstatic.net/E8FnN.jpg - link to code (because pasting code into this website seems a bit buggy or I'm just stupid lol)
Here's what I've got:
def main():
option = input("Login or sign up? L/S ")
if option == "L":
login()
if option == "S":
signup()
else:
print("Error - restart programm")
def login():
accounts = {"user123": ["pass123"],
"123user": ["123pass"], }
loginUser = input("Enter username: ")
loginPass = input("Enter password: ")
if loginUser in (accounts):
correct1 = 1
print("Correct")
else:
print("Incorrect")
if loginPass in (accounts[loginUser]):
correct2 = 1
print("Correct")
else:
print("Incorrect")
if correct1 and correct2 == 1:
print("")
print("Welcome to System")
print("")
def signup():
accounts = {"user123": ["pass123"],
"123user": ["123pass"], }
signUser = input("Enter custom username: ")
signPass = input("Enter custom password: ")
accounts.update({signUser: [signPass]})
main()
main()
You've got two different functions, login
and signup
, that each have their own local variables. The whole point of local variables is that they're only accessible within that function.
You either need to pass the values, or share the variables.
You can pass the values by using parameters (and return
statements, but you don't need those for this example). Create accounts
in the main
function, then pass it to the other two functions; that way, they'll all have the same dictionary.
But you have another problem that you also have to fix: You should not be calling main()
at the end of login
and signup
; just have them return
, and put a loop in main
. Otherwise, you're starting all over again each time; any local variables that got changed are going to go right back to their original values.
So:
def main():
accounts = {"user123": ["pass123"],
"123user": ["123pass"], }
while True:
option = input("Login or sign up? L/S ")
if option == "L":
login(accounts)
if option == "S":
signup(accounts)
else:
print("Error - restart programm")
return
def login(accounts):
print(accounts)
loginUser = input("Enter username: ")
loginPass = input("Enter password: ")
# etc.
if correct1 and correct2 == 1:
print("")
print("Welcome to System")
print("")
def signup(accounts):
signUser = input("Enter custom username: ")
signPass = input("Enter custom password: ")
accounts.update({signUser: [signPass]})
main()
I mentioned above that you need to "pass the values, or share the variables". What about the second one?
You can do this by creating global variables. (Or by creating a class, and using instance variables. Or by using nonlocal variables in a closure.) If you write global accounts
at the start of all three functions, they'll all refer to the same accounts
variable. But you still have to make sure not to keep reassigning it to the starting values over and over.