Ty Staszak Ty Staszak - 2 months ago 13
Python Question

Python module returning True

I'm writing Python for a kinda computer, and the login module always returns true, even if it's not in the

users
dictionary

cur_user = "null"
users = {
"Splavacado100": "20310"
}
def login():
good_login = 0
user_name = raw_input("Enter your username: ")
for user in users:
if user == user_name:
good_login = 1
if good_login == 1:
user_password = raw_input("Enter your password: ")
if users[user_name] == user_password:
return True
else:
return False
else:
return "None"
def OS():
log_att = False
log_att = login()
if log_att:
print "True"
else:
print "False"
OS()


Python 2.7.10

Answer

There is something wrong with your second method. I took it out and the first part works. I then took out the last else in the first method and seems to work. Try this

cur_user = "null"
users = {
    "Splavacado100": "20310"
}
def login():
    good_login = 0
    user_name = raw_input("Enter your username: ")
    for user in users:
        if user == user_name:
            good_login = 1
    if good_login == 1:
        user_password = raw_input("Enter your password: ")
        if users[user_name] == user_password:
            return True
        else:
            return False
def OS():
    log_att = False
    log_att = login()
     if log_att:
        print "True"
    else:
        print "False"
OS()
Comments