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.
-
4Use a dictionary? Oh, and i suppose it goes without mention that you shouldn't be doing this with plaintext passwords..UIlrvnd– UIlrvnd2014年04月10日 00:56:30 +00:00Commented Apr 10, 2014 at 0:56
-
This is going to be so easy to hack....0xcaff– 0xcaff2014年04月10日 01:18:30 +00:00Commented Apr 10, 2014 at 1:18
2 Answers 2
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..."
. . .
1 Comment
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
Explore related questions
See similar questions with these tags.