简体   繁体   中英

else statement issue, tkinter, python

Im having an issue with my logical/if/else statements it seems! the issue lies where if I type in a wrong password lets say but correct username, nothing happens, similar happens for both student and teacher, im just not really sure what to change. Thank you.

error:

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

You're assuming that the input password will always be in one of the tuples you've defined to contain the password. You can easily break this, simply pass in something that's not contained in one of the tuples. You're condition breaks because as you see in the error message it's trying to find the index of that value but it's not even in the tuple. Hence, the ValueError exception.

You need to check if the password is in the respective student/teacher passwords tuple as well as username, then you can check the indexes.

So, this would be if username in student_usernames and password in student_passwords: as one example.

Or, you could reverse the logic by DeMorgan's Law(s) and use:

if not (username in student_usernames or password in student_passwords)

student_passwords.index(password) assumes that password actually exists in student_passwords . You could use if username in student_usernames and password in student_passwords: instead or surround the whole block with try: except ValueError:

Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM