Python 使用if-else语句对照列表进行检查
我已经看过了大部分相关的问题,但似乎没有一个能给我提供我的课程所需要的想法Python 使用if-else语句对照列表进行检查,python,list,if-statement,Python,List,If Statement,我已经看过了大部分相关的问题,但似乎没有一个能给我提供我的课程所需要的想法 users = ["Block Harris", "Apple Mccoy", "Plays Terry", "Michael Strong", "Katie Blue"] nicknames = ["Block", "Apple", "Plays", "Michael"
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()
我在这里要做的是尝试测试这些成员或数据库中找到的用户的安全级别,然后使用下面的if-else语句根据他们的安全级别打印适当的消息。我不知道程序在做什么,但它没有根据列表中的级别评估找到的用户。例如,对于第一个人,列表中的级别相应地为5,但它会打印“find user==level[2]”的消息 您正在将“FoundUser”设置为“True”或“False”,但随后检查列表中的整数级别。它总是打印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
看看小麦答案,这是一个很好的建议。关于您的代码,您正在尝试使用
found\u user
访问安全级别found\u user
是一个布尔值,而不是一个级别。您应该使用安全性
变量
尝试打印级别信息时,请使用安全性
变量并对照级别进行检查,而不是对照包含不同用户级别的列表:
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.")
甚至
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."
编辑:
上面的消息作为一个以整数为键的词汇表是愚蠢的;这个更好
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.")
投了赞成票,但我可能会将“返回-1”替换为“不返回”。@Jeff Bauer适时地注意到并改变了。啊,你也注意到了+1快速抽签。
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.")