Beginner Python User Login System I started Python 3 days ago. This user login code is the first project I completed, not following along with a tutorial. The idea is that you can create a user and login with it. I am wondering if there are ways to make this code much more efficient or shorter. If it needs anything, I'd love to learn new things.
def choices():
    print(&quot;Please choose what you would like to do.&quot;)
    choice = int(input(&quot;For Signing Up Type 1 and For Signing In Type 2: &quot;))
    if choice == 1:
        print(&quot;Please Provide&quot;)
        username = str(input(&quot;Username: &quot;))
        f = open(&quot;usernames.txt&quot;, 'r')
        info = f.read()
        if username in info:
            print(&quot;Username unavailable. Please Try Again.&quot;)
            return print(choices())
        f.close()
        password = str(input(&quot;Password: &quot;))
        f = open(&quot;passwords.txt&quot;, 'r')
        info1 = f.read()
        f.close()
        f = open(&quot;usernames.txt&quot;, 'w')
        info = info + &quot; &quot; + username
        f.write(info)
        f.close()
        f = open(&quot;passwords.txt&quot;, 'w')
        info1 = info1 + &quot; &quot; + password
        f.write(info1)
        f.close()
        print(&quot;Congratulations! You have successfully created an account. You may now login in.&quot;)
        return print(choices())
    elif choice == 2:
        print(&quot;Please Provide&quot;)
        username = str(input(&quot;Username: &quot;))
        f = open(&quot;usernames.txt&quot;, 'r')
        info = f.read()
        if username in info:
            password = str(input(&quot;Password: &quot;))
            f = open(&quot;passwords.txt&quot;, 'r')
            info1 = f.read()
            if password in info1:
                print(&quot;Welcome Back &quot; + username + &quot;!&quot;)
            else:
                print(&quot;Your password is incorrect. Please try again.&quot;)
                return print(choices())
        else:
            print(&quot;Your username is either incorrect or nonexistent. You can either try again or create a new account.&quot;)
            return print(choices())
    else:
        raise TypeError


print(choices())
Use with to handle file operations
A common way to handle files is by creating a file instance with open statement and closing it when you are done with the handler, while this would work, it is prone to error. What if you forget to close the file? It leads to either the file not saving or the handler might be corrupted with garbage. with automatically closes the file once operations on it are done. A simple example is as follow
with open('usernames.txt', 'r') as f:
    f.read()

Recursion is expensive
Your choices function uses recursion which might become expensive. Recursion creates temporaries in the stack frame and as it recur more and more, your stack frame might eventually be filled, though it can be elegant to solve a problem with recursion, limit it usage. Any function that can be written recursively can also be written with an iterative approach. Iterative approach tends to be faster and does not create temporaries
Consider reducing the complexity of choices
Right now choice does way too much, signs up a user and also login a user. Your functions should do just one thing and do it very well. The name choices is very misleading, from the name, I would expect it to make a choice or rather I make a choice among a list of values, rather is suprisingly signs up and login a user. Avoid function side-effect. Function side-effect is when a function promises one thing and does another.
Use a dictionary pair
I really think a dictionary is needed in this scenario. Dictionaries as we all know are very fast. Storing a key-value pair of username and password makes the code clean and easy to reason about.
Detour

You might want to use open('filename.txt', 'a') when you want to append to a file, using 'w' overwrites the content of the file and is similar to calling truncate on the file.

When you want to recieve string input, no need for this statement str(input(&quot;Enter&quot;)), input automatically recieves strings.

Avoid variable names suchh as f


This is an editted version of the code
def signup():
    with open(&quot;usernames.txt&quot;, 'r') as user:
        usernames = user.read().splitlines()
    with open(&quot;passwords.txt&quot;, 'r') as pwd:
        passwords = pwd.read().splitlines()
    
    info = dict(zip(usernames, passwords))

    print(&quot;Please Provide&quot;)
    username = input(&quot;Username: &quot;)
    while username in info.keys():
        print(&quot;Username unavailable. Please Try Again.&quot;)
        username = input(&quot;Username: &quot;)      
    password = input(&quot;Password: &quot;)   
    
    with open(&quot;usernames.txt&quot;, 'a') as user:
        user.write(username + '\n')
    with open(&quot;passwords.txt&quot;, 'a') as pwd:
        pwd.write(password + '\n')
    
    print(&quot;Congratulations! You have successfully created an account. You may now login in.&quot;)

def login():
    with open(&quot;usernames.txt&quot;, 'r') as user:
        usernames = user.read().splitlines()
    with open(&quot;passwords.txt&quot;, 'r') as pwd:
        passwords = pwd.read().splitlines()
    
    info = dict(zip(usernames, passwords))
    username = input(&quot;Username: &quot;))
    password = input(&quot;Password: &quot;))

    if info.get(username, None) is None:
        print(&quot;Your username is either incorrect or nonexistent. You can either try again or create a new account.&quot;)
        signup()
        return

    if password != info[username]:
        print(&quot;Your password is incorrect. Please try again.&quot;)
        return

    print(&quot;Welcome Back &quot; + username + &quot;!&quot;)      

if __name__ == '__main__':
    print(&quot;Please choose what you would like to do.&quot;)
    choice = int(input(&quot;For Signing Up Type 1 and For Signing In Type 2: &quot;))
    if choice == 1:
        signup()
    elif choice == 2:
        login()