我已经完成了大部分相关问题,但似乎没有一个问题能让我对我的程序有所了解.
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()
Run Code Online (Sandbox Code Playgroud)
我在这里尝试做的是尝试测试每个成员的安全级别或在数据库中找到用户,然后使用下面的if-else语句根据其安全级别打印相应的消息.我不知道程序正在做什么,但它没有根据列表中的级别来评估找到的用户.例如,对于第一个人,列表中的级别相应地为5,但它会打印"找到用户==级别[2]"的消息.
您将"FoundUser"设置为"True"或"False",然后检查列表中的整数级别.它始终打印2,因为列表中的第2项是1.
建议:
您应该创建一个包含链接在一起的所有信息的类,而不是根据它们的顺序形成仅略微相关的列表:
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
Run Code Online (Sandbox Code Playgroud)