2016-04-14 29 views
0

我的邏輯/ if/else語句似乎有問題!問題在於如果我輸入了錯誤的密碼可以說但是正確的用戶名,什麼都沒有發生,對於學生和老師來說都是類似的,我只是不確定要改變什麼。謝謝。其他語句問題,tkinter,python

錯誤:

File "/Users/Sebastian/Desktop/DQS/login.py", line 43, in _login_btn_clickked 
    if (student_usernames.index(username) == student_passwords.index(password)): 
ValueError: tuple.index(x): x not in tuple 



from tkinter import * 
import tkinter.messagebox as tm 


class LoginFrame(Frame): 
    def __init__(self, master): 
     super().__init__(master) 

     self.label_1 = Label(self, text="Username") 
     self.label_2 = Label(self, text="Password") 

     self.entry_1 = Entry(self) 
     self.entry_2 = Entry(self, show="*") 

     self.label_1.grid(row=0, sticky=E) 
     self.label_2.grid(row=1, sticky=E) 
     self.entry_1.grid(row=0, column=1) 
     self.entry_2.grid(row=1, column=1) 

     self.checkbox = Checkbutton(self, text="Keep me logged in") 
     self.checkbox.grid(columnspan=2) 

     self.logbtn = Button(self, text="Login", command = self._login_btn_clickked) 
     self.logbtn.grid(columnspan=2) 

     self.pack() 

    def _login_btn_clickked(self): 
     #print("Clicked") 
     username = self.entry_1.get() 
     password = self.entry_2.get() 

     #print(username, password) 

     student_usernames = ("C100", "C200", "C300") 
     student_passwords = ("PASS", "PASS1", "PASS2") 

     teacher_usernames = ("T100", "T200", "T300") 
     teacher_passwords = ("TPASS", "TPASS1", "TPASS3") 


     if username in student_usernames: 
      if (student_usernames.index(username) == student_passwords.index(password)): 
       tm.showinfo("Login info", "Welcome Student") 
      else: 
       tm.showerror("Login error", "Incorrect information") 
     elif username in teacher_usernames: 
      if (teacher_usernames.index(username) == teacher_passwords.index(password)): 
       tm.showinfo("Login info", "Welcome Teacher") 
      else: 
       tm.showerror("Login error", "Incorrect information") 
     else: 
      tm.showerror("Login error", "Incorrect information") 



root = Tk() 
lf = LoginFrame(root) 
root.mainloop() 
+0

你可以發佈你測試它的shell輸出嗎? – Tommy

+0

'student_passwords.index(密碼)'假定''password'實際存在於'student_passwords'中。你可以使用'如果用戶名在student_usernames和密碼在student_passwords:'中,或者用'try/except'包圍整個塊, – jDo

回答

0

student_passwords.index(password)假定passwordstudent_passwords確實存在。你可以用if username in student_usernames and password in student_passwords:來代替或者用try: except ValueError:

圍繞整個塊。

if username in student_usernames and password in student_passwords: 
    if (student_usernames.index(username) == student_passwords.index(password)): 
     tm.showinfo("Login info", "Welcome Student") 
    else: 
     tm.showerror("Login error", "Incorrect information") 
elif username in teacher_usernames and password in teacher_passwords: 
    if (teacher_usernames.index(username) == teacher_passwords.index(password)): 
     tm.showinfo("Login info", "Welcome Teacher") 
    else: 
     tm.showerror("Login error", "Incorrect information") 
else: 
    tm.showerror("Login error", "Incorrect information") 
0

你假設輸入密碼總是會在你定義爲包含密碼的元組之一。你可以很容易地破解這個,只需傳入一個不包含在元組中的東西。你是條件中斷,因爲正如你在錯誤信息中看到的,它試圖找到該值的索引,但它甚至不在元組中。因此,ValueError例外。

您需要檢查密碼是否在相應的學生/教師密碼元組以及用戶名中,然後才能檢查索引。

因此,這將是if username in student_usernames and password in student_passwords:作爲一個例子。

或者,你可以通過德摩根定律(S)和使用反向邏輯:

if not (username in student_usernames or password in student_passwords)