其他语句问题,tkinter,python
问题描述:
我的逻辑/ 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()
答
student_passwords.index(password)
假定password
在student_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")
答
你假设输入密码总是会在你定义为包含密码的元组之一。你可以很容易地破解这个,只需传入一个不包含在元组中的东西。你是条件中断,因为正如你在错误信息中看到的,它试图找到该值的索引,但它甚至不在元组中。因此,ValueError
例外。
您需要检查密码是否在相应的学生/教师密码元组以及用户名中,然后才能检查索引。
因此,这将是if username in student_usernames and password in student_passwords:
作为一个例子。
或者,你可以通过德摩根定律(S)和使用反向逻辑:
if not (username in student_usernames or password in student_passwords)
你可以发布你测试它的shell输出吗? – Tommy
'student_passwords.index(密码)'假定''password'实际存在于'student_passwords'中。你可以使用'如果用户名在student_usernames和密码在student_passwords:'中,或者用'try/except'包围整个块, – jDo