开发者

Checking against a list using if else statment

开发者 https://www.devze.com 2023-02-14 10:11 出处:网络
I\'ve gone through most of the related questions and none of them seem to give me the idea I need for my program.

I've gone through most of the related questions and none of them seem to give me the idea I need for my program.

users = ["Block Harris",
         "Apple Mccoy",
         "Plays Terry",
         "Michael Strong",
         "Katie Blue"]

nicknames = ["Block",
             "Apple",
             "Plays",
             "Michael",
             "Katie"]


passwords = ["abc",
             "def",
             "ghi",
             "jkl",
             "mno"]

levels = [5,2,1,4,3]

security = 0
found_user = False

username = ""
while not username:
    username = input("开发者_JS百科Username: ")

password = ""
while not password:
    password = input("Password: ")

for i in range(5):
    if username == users[i]:      
        found_user = True        
        if password == passwords[i]:
            security = levels[i]
            print("Welcome, ", nicknames[i])
            break                  
        else:
            print("Sorry, you don't know the password.")  

if found_user == levels[0]:
    print("Security level 1: You have little privelages. Congratulations.")
elif found_user == levels[1]:
    print("Security level 2: You have more than little privelages. Congratulations.")
elif found_user == levels[2]:
    print("Security level 3: You have average privelages. Congratulations.")
elif found_user == levels[3]:
    print("Security level 4: You have more than average privelages. Congratulations.")
elif found_user == levels[4]:
    print("Security level 5: You have wizard privelages. Congratulations.")

else:
    print("Apparently you don't exist.")

data_network()

What I am trying to do here is trying to test for the security level of each of these members or found users in the database and then printing an appropriate message based on their security level using the if-else statements below. I have no idea what the program is doing but it's not evaluating the found user according to their level in the list. For example, for the first person, the level in the list is 5 accordingly, but it prints message for "found user == level[2]".


You're setting "FoundUser" to "True" or "False" but then checking against a level in a list that is an Integer. It's always printing 2 because your 2nd item in the list is 1.

Suggestion:

Instead of forming lists that are only marginally related according to their ordering, you should come up with a class that contains all the information linked together:

class User(object):
    def __init__(self, name, nickname, password, security_level):
        self.name = name
        self.nick = nickname
        self.pw = password
        self.level = security_level

    def authenticate(self, name, password):
        return self.name == name and self.pw == password

    def getLevel(self, name, password):
        if self.authenticate(name, password):
            print("Welcome", self.nick)
            return self.level
        else:
            return None


Have a look at wheaties answer which is good advice. Regarding your code, you are trying to use found_user to access the security level. found_user is a boolean not a level. You should use your security variable.

When trying to print the level information, use the security variable and check against the level, not the list containing the levels of the different users:

if security == 1:
    print("Security level 1: You have little privelages. Congratulations.")
elif security == 2:
    print("Security level 2: You have more than little privelages. Congratulations.")
elif security == 3:
    print("Security level 3: You have average privelages. Congratulations.")
elif security == 4:
    print("Security level 4: You have more than average privelages. Congratulations.")
elif security == 5:
    print("Security level 5: You have wizard privelages. Congratulations.")

else:
    print("Apparently you don't exist.")

Or even

   levels_info = [
        "Security level 1: You have little privelages. Congratulations.",
        "Security level 2: You have more than little privelages. Congratulations.",
        "Security level 3: You have average privelages. Congratulations.",
        "Security level 4: You have more than average privelages. Congratulations.",
        "Security level 5: You have wizard privelages. Congratulations."
    ]

    if security in levels_info:
        print levels_info[security]
    else
        print "Apparently you don't exist."


dic = {"Block Harris":("Block","abc",5),
       "Apple Mccoy":("Apple","def",2),
       "Plays Terry":("Plays","ghi",1),
       "Michael Strong":("Michael","jkl",4),
       "Katie Blue":("Katie","mno",3)}

message = dict(zip(1,2,3,4,5),("Security level 1: You have little priveleges. Congratulations.",
                               "Security level 2: You have more than little priveleges. Congratulations.",
                               "Security level 3: You have average priveleges. Congratulations.",
                               "Security level 4: You have more than average priveleges. Congratulations.",
                               "Security level 5: You have wizard priveleges. Congratulations."))


username = ""
while not username:
    username = raw_input("Username: ")

password = ""
while not password:
    password = raw_input("Password: ")

try:
    if password==dic[username][1]:
        security = dic[username][2]
        print("Welcome, ", dic[username][0])
        print(message[security])
    else:
        print("Sorry, you don't know the password.")
except:
    print("You are not registered")

EDIT:

the above message as a dictionnary with integers as keys is stupid; this one is better

message = ("Security level 1: You have little priveleges. Congratulations.",
           "Security level 2: You have more than little priveleges. Congratulations.",
           "Security level 3: You have average priveleges. Congratulations.",
           "Security level 4: You have more than average priveleges. Congratulations.",
           "Security level 5: You have wizard priveleges. Congratulations.")
0

精彩评论

暂无评论...
验证码 换一张
取 消