0
   def runUser():
       x = raw_input("Enter your username:")
       y = raw_input("Enter your password :")
       users = [x == "aeduun" and y == "1234", x == "paul" and y == "fifty"]
       raw_input("")
       if x == users[] and y == users[]:#x == "aeduun" and y == "1234":
           print "you are now logged in"
       elif x == "Mercuryisle" and y == "shrek":
           print "Your account has expired..." \
                 "You will now bw taken back to the login page"
           time.sleep(5)
       return runUser()

Here is the code that i have compiled as part of a minor testing project for python 2.7. In this i am attempting to forge a basic login with a list of variables for passcodes and usernames. The function is able to read the relationship between the variables but consists of an error to the reference of different list items. When i launch the program all that occurs is the call for an invalid username is invoked (code not displayed). I would like some advice on how to initialize the indices for the list items in a way to which they will read as part of the if statement.

2
  • 4
    Use a dictionary? Oh, and i suppose it goes without mention that you shouldn't be doing this with plaintext passwords.. Commented Apr 10, 2014 at 0:56
  • This is going to be so easy to hack.... Commented Apr 10, 2014 at 1:18

2 Answers 2

3

It's not secure to store passwords in plaintext, but given that, I think you might benefit from the use of tuples and sets, i.e.

users = set([("aeduun","1234"), ("paul","fifty")])
expired_users = set([("Mercuryisle", "shrek")])
. . .
if (x,y) in users:
    print "you are now logged in
elif (x,y) in expired_users:
    print "Your account has expired..."
. . .
Sign up to request clarification or add additional context in comments.

1 Comment

The dictionary suggested by aj8uppal makes usernames the unique identifier, which works for changed passwords.
1
def runUser():
   x = raw_input("Enter your username: ")
   y = raw_input("Enter your password: ")
   users = {"aeduun":"1234", "paul":"fifty"}
   raw_input("")
   if x in users:
       if y == users[x]:
           print "you are now logged in"
   elif x == "Mercuryisle" and y == "shrek":
       print "Your account has expired..." \
             "You will now bw taken back to the login page"
       time.sleep(5)
   return runUser()

Runs as:

>>> runUser()
Enter your username: aeduun
Enter your password: 1234

you are now logged in
Enter your username: Mercuryisle
Enter your password: shrek

Your account has expired...You will now bw taken back to the login page
Enter your username: 

And just a suggestion, use getpass.getpass() to get the password, it prevents character echo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.