2011-03-02 151 views
0

我已經經歷了大部分相關的問題,並且他們都沒有給我提供我需要的程序的想法。使用if if語句檢查列表

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,但它會輸出「找到用戶==級別[2]」的消息。

回答

7

您正在將「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 
+1

Upvoted,但我可能會將「return -1」替換爲「return None」。 – 2011-03-02 01:31:52

+0

@Jeff Bauer Duly注意到並改變了。 – wheaties 2011-03-02 01:33:37

2

看一看麥片的答案,這是很好的建議。關於您的代碼,您正嘗試使用found_user來訪問安全級別。 found_user是一個布爾不是一個級別。你應該使用你的security變量。

當要打印的級別信息,使用security變量和對證的水平,不包含用戶的不同級別的列表:

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." 
+0

啊,你也注意到了。 +1快速抽獎。 – senderle 2011-03-02 01:42:39

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") 

編輯:

上述消息作爲一個與dictionnary在泰格作爲關鍵是愚蠢的;這一個更好

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.")