mardi 6 juin 2017

How can a dictionary and if function co-exist without conflicting eachother? (Tkinter)

I have written a program using Python's tkinter module. The premise is that after giving a password, it produces a response depending on how strong it is.

from tkinter import *

def click():
    entered_text = entry.get()
    if len(entered_text) < 9:
        output.delete(0.0, END)
        output.insert(END, 'This is too short')     #Click function
    elif entered_text in pass_Type:
        strength = pass_Type[entered_text]
        output.delete(0.0, END)
        output.insert(END, strength)
    else:
        output.delete(0.0, END)
        output.insert(END, "This password is acceptable!") #When the password is ok

Password = Tk()
Password.title('Password tester') 

label = Label(Password, text="Password:")
label.grid(row=0, column=0, sticky=W)    #Entry label

entry = Entry(width=20, bg='light blue')
entry.grid(row=1, column=0, sticky=W)     #Entry box

Button(Password, text='SUBMIT',width=5, command=click).grid(row=2,column=0, sticky=W)  #Button

label = Label(Password, text='Strength:')
label.grid(row=4, column=0, sticky=W)          #Output label

output = Text(Password, width=75, height=6, wrap=WORD, background='light blue')
output.grid(row=5, column=0, columnspan=2, sticky=W)                              #Output box 

pass_Type = {
  'Password': 'This is a very predicatable password. You should incorporate numbers',
  'password': 'This is a very predicatable password. You should incorporate numbers and capital letters',  #Common password glossary 
  '12345': 'Try and incorporate some letters',
  'qwerty': 'Try to jumble up your letters so the password is not so predictable.'
  }

Password.mainloop()

This code runs prefectly fine. However, when I run one of the common passwords from the dictionary ('pass_type,')the message for the length runs.

Thus, I tried adding this along with the rest of 'def click():.'

elif strength and len(entered_text) < 9:
       output.delete(0.0, END)
       output.insert(END, strength)

This still returns the message 'this is too short' from the length check, despite the fact I assigned it to strength.

When checking to see if it still works with a longer password, I recieved the following error:

File "", line 1549, in __call__
    return self.func(*args)
  File "", line 12, in click
    elif strength and len(entered_text) < 9:
UnboundLocalError: local variable 'strength' referenced before assignment

How can I get the common password still assigned with their attributed dictionary sentence? And how can I solve the error when entering a longer password?

Aucun commentaire:

Enregistrer un commentaire