Guest User

Untitled

a guest
Nov 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. ##
  2.  
  3. ##
  4.  
  5. ##
  6.  
  7.  
  8. # Functions
  9.  
  10. def validate_password(first_pwd, second_pwd):
  11. """
  12. validates if password is acceptable
  13. """
  14.  
  15. min_length = 8
  16.  
  17. number_of_tests = 6
  18. test1 = 0 #Both entered passwords are identical.
  19. test2 = 0 #The password is >= 8 characters in length.
  20. test3 = 0 #if first and last chars are alpha then both must be different
  21. test4 = 0 #There are no more than 2 vowels in the password.
  22. test5 = 0 #The password has at least 1 alphabetic character
  23. #in either upper or lower case.
  24. test6 = 0 #Not all alphabetic characters are in the same
  25. #case (either all upper or all lower).
  26.  
  27. # test1
  28. if first_pwd == second_pwd:
  29. test1 = 1
  30.  
  31. password = first_pwd
  32. pwd_length = len(password)
  33.  
  34. # test2
  35. if pwd_length >= 8:
  36. test2 = 1
  37.  
  38. # test3
  39.  
  40. same = 0
  41.  
  42. if password[0].isalpha() and password[pwd_length-1].isalpha():
  43. if password[0] == password[pwd_length-1].upper():
  44. same = 1
  45. if password[0] == password[pwd_length-1].lower():
  46. same = 1
  47. if same == 0:
  48. test3 = 1
  49.  
  50. else:
  51. test3 = 1
  52.  
  53.  
  54. # test4
  55. vowels = ["a","e","i","o","u","A","E","I","O","U"]
  56. count = 0
  57. max_vowels = 2
  58. for vowel in vowels:
  59. count = count + password.count(vowel)
  60.  
  61. if count <= max_vowels:
  62. test4 = 1
  63.  
  64. alpha_chars = []
  65.  
  66. # test5
  67. for char in password:
  68. if char.isalpha():
  69. alpha_chars.append(char)
  70. test5 = 1
  71.  
  72.  
  73. # test6
  74. alpha_count = len(alpha_chars)
  75. upper_count = 0
  76. lower_count = 0
  77. for char in alpha_chars:
  78. if char == char.upper():
  79. upper_count = upper_count + 1
  80. if char == char.lower():
  81. lower_count = lower_count + 1
  82.  
  83. if alpha_count != upper_count and
  84. alpha_count != lower_count:
  85. test6 = 1
  86.  
  87. test_count = test1 + test2 + test3 + test4
  88. + test5 + test6
  89.  
  90. if test_count == number_of_tests:
  91. return True
  92.  
  93.  
  94.  
  95. return False
  96.  
  97. # Program Code
  98.  
  99. from tkinter import *
  100.  
  101. root_window = Tk()
  102.  
  103. # window title
  104. root_window.title("Title")
  105.  
  106. # window size
  107. root_window.geometry("400x200")
  108.  
  109. # label asking for password
  110. pwd_label = Label(root_window, text="Please enter your password")
  111. pwd_label.pack()
  112.  
  113. # pwd entry boxes
  114.  
  115. pwd1 = StringVar()
  116. pwd1_text = Entry(root_window,textvariable=pwd1)
  117. pwd1_text.pack()
  118.  
  119. pwd2 = StringVar()
  120. pwd2_text = Entry(root_window,textvariable=pwd2)
  121. pwd2_text.pack()
  122.  
  123. # pwd check button
  124. check_btn = Button(root_window,text="Change Password",command=lambda: validate_password(pwd1, pwd2))
  125. check_btn.pack()
  126.  
  127.  
  128.  
  129.  
  130. root_window.mainloop()
  131.  
  132. ##
Add Comment
Please, Sign In to add comment