-3

I am making an account code in python and it works but it is really long and I want to know if there is a way to shorten it. Here is my code:

user1 = "Jeff"
user2 = "Bob"

password1 = "Password"
password2 = "Lol"

username = input("Login: >> ")
password = input("Password: >> ")

if username == user1:
    if password == password1:
        print ("Access granted")
        print ("Welcome to the system!")
        home()

    else:
        print ("Access denied")
        print ("Try again!")
        print ("\n")
        time.sleep(2)
        login()

elif username == user2:
    if password == password2:
        print ("Access granted")
        print ("Welcome to the system!")
        home()
    else:
        print ("Access denied")
        print ("Try again!")
        print ("\n")
        time.sleep(2)
        login()

else:
    print ("Access denied")
    print ("Try again!")
    print ("\n")
    time.sleep(2)
    login()

The more accounts I create the longer longer it will get. Is there a way to shorten or simplify this?

wpercy
  • 9,636
  • 4
  • 33
  • 45
S.James
  • 19
  • 7
  • Have you ever heard of defining a function? Most any time that you repeat a bunch of lines, it's best to group all that repeated code in a function and call the function. Any python guide will teach you how to do that. – Davy M Nov 17 '17 at 15:58
  • 3
    Like Dijkstra says: "*two or more, use a `for`*" – Willem Van Onsem Nov 17 '17 at 15:58
  • 1
    You'll need to learn to use lists of some other data structure, or start moving stuff to a function. This is too broad for here though. If you code works and it's complete, you can post it on Code Review with a good description to have UT reviewed. – Carcigenicate Nov 17 '17 at 15:59
  • The design I recommend using is a dictionary. Each time some creates a new account, log their info in the user dictionary. Then, when some attempts to login, logging them in is as simple as testing if their credentials are in your user dictionary. – Christian Dean Nov 17 '17 at 16:00
  • Thanks to everyone who has helped me with this code! I really appreciate it! – S.James Nov 17 '17 at 16:31

2 Answers2

0

Here is a version of your program that uses a dictionary to store user names with their associated passwords. This way, you can add more users and passwords to the users dictionary, and you do not need to add any more code.

import time

users = {
    "Jeff": "Password",
    "Bob": "Lol"}

def login():
    username = input("Login: >> ")
    password = input("Password: >> ")
    if username in users and password == users[username]:
        print("Access granted")
        print("Welcome to the system!")
        return True

    print("Access denied")
    print("Try again!")
    print()
    return False

while not login():
    pass

home()
gammazero
  • 773
  • 6
  • 13
  • You can also do `login_message = ('Access denied', 'Access granted')` and then `is_successful = username in users and password == users[username]; print(login_message[is_successful]; return is_successful`. The loop can also be simplified to `while True: if login(): break`. – Reti43 Nov 17 '17 at 16:14
  • Thanks for the help! I really appreciate it, this will really help me improve my code. – S.James Nov 17 '17 at 16:22
0

I'd separate the user and password checks:

users = {
    "Jeff": "Password",
    "Bob": "Lol"}

def login(user=None):

    # User
    if not user:
        user = input("Login: >> ").title()
        if not users.get(user):
            print ("No such user")
            return None

    # Password
    password = input("Password: >> ")
    if password == users[user]:
        print ("Access granted")
        print ("Welcome to the system!")
        return user
    else:
        print("Access denied")
        print("Try again!")
        return login(user=user)

while True:
    logged_in_user = login()
    if logged_in_user:
        print("Welcome {}".format(logged_in_user))
        break
Anton vBR
  • 18,287
  • 5
  • 40
  • 46