-1

Im learning python and i want to have this function "checkusername" check if the user is within a user list. but it dosent seem to check the list.

userlist = ["mint"]
ilogin = " "
ipass = " "

def displayWelcome():
    print "Welcome to Mints Server"
    print " "

def getUserLogin():
    return raw_input("Please enter in your login id: ")
def getUserPass():
    return raw_input("Please enter in your login password: ")
def displaylogincred(ID,password):
    print "ID: " + ID
    print "Pass" + password
def checkUsername(user):
    global userlist
    if user == userlist:
        return "Login Successful"
    else:
        return "Login Incorrect"

displayWelcome()
ilogin = getUserLogin()
ipass = getUserPass()
print checkUsername(ilogin)
Mint
  • 53
  • 1
  • 2

1 Answers1

6

What you are doing in this line:

if user == userlist:

Is checking whether or not user and userlist are equal. This is not what you want. You want to check if a value equal to user is inside userlist, not whether or not user is the same as the entire userlist.

To check if a list contains a specific value, you use in:

if user in userlist:
    return "Login Successful"
else:
    return "Login Incorrect"

Note that the global userlist you have before your if is completely unnecessary. userlist is not declared in any function or class so it is already accessible everywhere in your module.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • 1
    To clarify, the double equals `==` checks to see if `user` is equivalent to `userlist` and the keyword `in` iterates through the collection and returns `True` as soon as an instance within the collection `userlist` is equivalent to `user` – VoNWooDSoN Aug 17 '17 at 20:34