Home > Back-end >  how to make sign up and login program in python?
how to make sign up and login program in python?

Time:09-24

Well im trying to make program that people can assign a username and password and then sign in. I was thinking about making 2 functions one for sign up and one for login but it seems that it can't work because I assign a username in register part but when the sign in function runs it says that username is not defind. Do you have any idea what should I do?

def register():
    names=[]
    usernames=[]
    passwords=[]
    names.append(input("Enter your name:"))
    usernames.append(input("choose your username:"))
    passwords.append(input("choose your password:"))
    return usernames
def login(usernames,passwords):
    usernames=[]
    passwords=[]
    password=""
    username=""
    username=input("Enter your username:")
    password=input("Enter your Password:")
    if password==passwords[usernames.index(username)]:
       print("welcome")
    else:
       print("incorrect!")

account_ans=""
while True:
    account_ans=input("choose:  a)Sign Up     b)login and shop     c)quit")
    if account_ans=="a":
       register()
    if account_ans=="b":
       password=""
       username=""
       usernames=[]
       passwords=[]
       login(usernames,passwords)
    if account_ans=="c":break

CodePudding user response:

Try this

usernames = []
passwords = []
names = []


def register():
    names.append(input("Enter your name:"))
    usernames.append(input("choose your username:"))
    passwords.append(input("choose your password:"))


def login():
    username = input("Enter your username:")
    password = input("Enter your Password:")
    if username in usernames and password in passwords:
        print("welcome")
    else:
        print("incorrect!")


while True:
    account_ans = input("choose:  a)Sign Up     b)login and shop     c)quit ")
    if account_ans == "a":
        register()
    if account_ans == "b":
        login()
    if account_ans == "c":
        break

CodePudding user response:

Your register() function returns a list object username but you don't catch that into a variable so it's lost. All of that information you enter into register() disappears as soon as the function is done because of this.

So 1) Capture your return

if account_ans=="a":
   username=register()

When you get into the account_ans="b" portion of your code you then reset this username variable emptying it completely. So when you pass username to the login() function it's just an empty list. You can't log someone in that doesn't exist and so you get your error here.

So 2) Don't empty out your username list:

if account_ans=="b":
   login(usernames,passwords)

Of course there are more issues here, but those are the most glaring. I would suggest not passing your username and password lists around as it will get overwhelming try to juggle them through the spaghetti of code. Instead declare them at the top of your code and use them as globals. Daniel's answer on this same question shows a good way to pull this off.

Ultimately, the real problem here is your inability to debug the issue. A good starting point would be to toss some print() in there and see what's in these variables at different times in your code. You would see if you did a print(usernames) inside of your login function before the error pops that the usernames list is empty at this point in code execution. Then you can backtrack and figure out why usernames would be empty. You will hit a million errors like this as you are building out this code so learning how to debug now is going to be key to your success.

CodePudding user response:

In the register function you can you the split function to get a list of inputs then you can return the username and password variables. I also suggest using unique variable names just in case they override each other.

def register():
    names = input("Names: ").split(" ")
    username_list = input("Usernames: ").split(" ")
    password_list = input("Passwords: ").split(" ")

    return username_list, password_list

In the login function you can take inputs for the username and password then check it through the if condition. In this case you might have to handle an exception since if the user enters a wrong username you get a ValueError or alternatively you can just check both the username and the password from an if condition.

def login(usernames,passwords):
    username = input("Enter your username:")
    password = input("Enter your Password:")
    try:
        if password == passwords[usernames.index(username)]:
            print("welcome")
        else:
            print("incorrect!")
    except ValueError:
        print("Invalid username")

Here you can you elif and else rather than only using if conditions and when you call the register function you have to assign the values returned by it to 2 variables so you can use it in the login function.

while True:
    account_ans = input("choose:  a)Sign Up     b)login and shop     c)quit: ")
    if account_ans == "a":
       usernames, passwords = register()
       
    elif account_ans=="b":
       login(usernames,passwords)
       
    else:
        break
  • Related