0

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("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]".

Geoffrey
5,42910 gold badges49 silver badges81 bronze badges
asked Mar 2, 2011 at 1:11
0

3 Answers 3

7

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
Hugh Bothwell
56.9k9 gold badges90 silver badges103 bronze badges
answered Mar 2, 2011 at 1:23
2
  • 1
    Upvoted, but I'd probably replace "return -1" with "return None". Commented Mar 2, 2011 at 1:31
  • @Jeff Bauer Duly noted and changed. Commented Mar 2, 2011 at 1:33
2

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."
answered Mar 2, 2011 at 1:34
1
  • Ah, you noticed it too. +1 for a quick draw. Commented Mar 2, 2011 at 1:42
0
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.")
answered Mar 2, 2011 at 1:47
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.