Skip to main content
Tweeted twitter.com/StackCodeReview/status/1334694093134442496
added 4 characters in body
Source Link
Marc
  • 5.7k
  • 2
  • 14
  • 35
def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Signing Up Type 1 and For Signing In Type 2: "))
    if choice == 1:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            print("Username unavailable. Please Try Again.")
            return print(choices())
        f.close()
        password = str(input("Password: "))
        f = open("passwords.txt", 'r')
        info1 = f.read()
        f.close()
        f = open("usernames.txt", 'w')
        info = info + " " + username
        f.write(info)
        f.close()
        f = open("passwords.txt", 'w')
        info1 = info1 + " " + password
        f.write(info1)
        f.close()
        print("Congratulations! You have successfully created an account. You may now login in.")
        return print(choices())
    elif choice == 2:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            password = str(input("Password: "))
            f = open("passwords.txt", 'r')
            info1 = f.read()
            if password in info1:
                print("Welcome Back " + username + "!")
            else:
                print("Your password is incorrect. Please try again.")
                return print(choices())
        else:
            print("Your username is either incorrect or nonexistent. You can either try again or create a new account.")
            return print(choices())
    else:
        raise TypeError


print(choices())
```
def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Signing Up Type 1 and For Signing In Type 2: "))
    if choice == 1:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            print("Username unavailable. Please Try Again.")
            return print(choices())
        f.close()
        password = str(input("Password: "))
        f = open("passwords.txt", 'r')
        info1 = f.read()
        f.close()
        f = open("usernames.txt", 'w')
        info = info + " " + username
        f.write(info)
        f.close()
        f = open("passwords.txt", 'w')
        info1 = info1 + " " + password
        f.write(info1)
        f.close()
        print("Congratulations! You have successfully created an account. You may now login in.")
        return print(choices())
    elif choice == 2:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            password = str(input("Password: "))
            f = open("passwords.txt", 'r')
            info1 = f.read()
            if password in info1:
                print("Welcome Back " + username + "!")
            else:
                print("Your password is incorrect. Please try again.")
                return print(choices())
        else:
            print("Your username is either incorrect or nonexistent. You can either try again or create a new account.")
            return print(choices())
    else:
        raise TypeError


print(choices())
def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Signing Up Type 1 and For Signing In Type 2: "))
    if choice == 1:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            print("Username unavailable. Please Try Again.")
            return print(choices())
        f.close()
        password = str(input("Password: "))
        f = open("passwords.txt", 'r')
        info1 = f.read()
        f.close()
        f = open("usernames.txt", 'w')
        info = info + " " + username
        f.write(info)
        f.close()
        f = open("passwords.txt", 'w')
        info1 = info1 + " " + password
        f.write(info1)
        f.close()
        print("Congratulations! You have successfully created an account. You may now login in.")
        return print(choices())
    elif choice == 2:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            password = str(input("Password: "))
            f = open("passwords.txt", 'r')
            info1 = f.read()
            if password in info1:
                print("Welcome Back " + username + "!")
            else:
                print("Your password is incorrect. Please try again.")
                return print(choices())
        else:
            print("Your username is either incorrect or nonexistent. You can either try again or create a new account.")
            return print(choices())
    else:
        raise TypeError


print(choices())
```
def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Signing Up Type 1 and For Signing In Type 2: "))
    if choice == 1:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            print("Username unavailable. Please Try Again.")
            return print(choices())
        f.close()
        password = str(input("Password: "))
        f = open("passwords.txt", 'r')
        info1 = f.read()
        f.close()
        f = open("usernames.txt", 'w')
        info = info + " " + username
        f.write(info)
        f.close()
        f = open("passwords.txt", 'w')
        info1 = info1 + " " + password
        f.write(info1)
        f.close()
        print("Congratulations! You have successfully created an account. You may now login in.")
        return print(choices())
    elif choice == 2:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            password = str(input("Password: "))
            f = open("passwords.txt", 'r')
            info1 = f.read()
            if password in info1:
                print("Welcome Back " + username + "!")
            else:
                print("Your password is incorrect. Please try again.")
                return print(choices())
        else:
            print("Your username is either incorrect or nonexistent. You can either try again or create a new account.")
            return print(choices())
    else:
        raise TypeError


print(choices())
Source Link

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("Please choose what you would like to do.")
    choice = int(input("For Signing Up Type 1 and For Signing In Type 2: "))
    if choice == 1:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            print("Username unavailable. Please Try Again.")
            return print(choices())
        f.close()
        password = str(input("Password: "))
        f = open("passwords.txt", 'r')
        info1 = f.read()
        f.close()
        f = open("usernames.txt", 'w')
        info = info + " " + username
        f.write(info)
        f.close()
        f = open("passwords.txt", 'w')
        info1 = info1 + " " + password
        f.write(info1)
        f.close()
        print("Congratulations! You have successfully created an account. You may now login in.")
        return print(choices())
    elif choice == 2:
        print("Please Provide")
        username = str(input("Username: "))
        f = open("usernames.txt", 'r')
        info = f.read()
        if username in info:
            password = str(input("Password: "))
            f = open("passwords.txt", 'r')
            info1 = f.read()
            if password in info1:
                print("Welcome Back " + username + "!")
            else:
                print("Your password is incorrect. Please try again.")
                return print(choices())
        else:
            print("Your username is either incorrect or nonexistent. You can either try again or create a new account.")
            return print(choices())
    else:
        raise TypeError


print(choices())
```