I wanted to create a basic login scenario for practice but I am always getting it wrong

Question:

I wanted to create a basic login scenario for practice but I am always getting it wrong. I want to use lists and want to append nicknames to lists but I am unable to append nicknames after signup. My idea here is to create a list with all nicknames and another list with all passwords and use login feature if nickname belongs to first list and password to second list. But I am unable to add nicknames and passwords to those lists using append.

Somebody please help.

I am still a beginner and just started learning python this week. Please help me using simple features like lists append if else functions etc.

Here is my code

def main():
    list1 = []
    list2 = []
    print("S M A R T    L O G I N    S Y S T E M")
    op1 = input(print("Do you want to login or signup?"))
    if op1 == "signup":
        id2 = input(print("What do you want your nickname to be : "))
        if id2 in list1:
            print("Sorry, that username is taken. If it is yours try login option")
            main()
        else:
            print("Great your username is " + id2)
            pw2 = input(print("what do you want to set your password as : "))
            print("Great " + id2 + ", Your account is created, reload to login")
            list1 = list1.append(id2)
            list2 = list2.append(pw2)
            main()
    elif op1 == "login":
        id1 = input(print("Enter your nickname : "))
        if id1 in list1:
            pw1 = input(print("Enter your password : "))
            if pw1 in list2:
                print("Welcome " + id1 + "nYou are now logged in")
                exit()
            else:
                print("You have entered incorrect password")
                main()
        else:
            print("You have entered incorrect id. Check again or signup")
            main()
    else:
        print("You can only answer in login or signup!!")
        main()
main()
Asked By: SASANK

||

Answers:

You are defining the two list inside the main function. Now when the nickname is taken, you call main again. This recursion creates seperate lists every time. This means the Llist isnt recognised by the rest of the programm.
Either you use a while loop in your signup process or you define the lists outside of the main function to make them global.
If you have questions about the solutions feel free to ask

EDIT:

list1 = []
list2 = []
def main():
    global list1
    global list2
    print("S M A R T    L O G I N    S Y S T E M")
    op1 = input(print("Do you want to login or signup?"))
    if op1 == "signup":
        id2 = input(print("What do you want your nickname to be : "))
        if id2 in list1:
            print("Sorry, that username is taken. If it is yours try login option")
            main()
        else:
            print("Great your username is " + id2)
            pw2 = input(print("what do you want to set your password as : "))
            print("Great " + id2 + ", Your account is created, reload to login")
            list1.append(id2)
            list2.append(pw2)
            main()
    elif op1 == "login":
        id1 = input(print("Enter your nickname : "))
        if id1 in list1:
            pw1 = input(print("Enter your password : "))
            if pw1 in list2:
                print("Welcome " + id1 + "nYou are now logged in")
                exit()
            else:
                print("You have entered incorrect password")
                main()
        else:
            print("You have entered incorrect id. Check again or signup")
            main()
    else:
        print("You can only answer in login or signup!!")
        main()
main()
Answered By: Jon_Kle
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.