Reputation: 1
I am creating a program that checks passwords to see if they are strong enough:
As you can see below this is only some of my code and I have a menu, and a password checker, the only problem is that the checker doesn't seem to be working as when I enter a password, its always saying its a strong password even though it does not contain any caps or numbers or symbols. Below is my code so far.
import random #importing random
import time #importing time
global Menu
def Menu():
global optionchoice #globalising variable optionchoice so that it can be used inside of another def of the code
print("Hey, Welcome to PassWordChecker.")
time.sleep(2) #delaying the next line for 2 seconds
print("Please choose one of the options below." )
time.sleep(1)
print("MENU:")
print("***")
print(" press 1 once for : password checker ")
print(" press 2 twice for : password generator")
print(" press 3 two or three times to : Quit ")
print(" ***") #creating a basic menu
optionchoice = (input("which option would you like to choose?"))
def checkpasswordandinput():
global optionchoice
optionchoice = optionchoice
if optionchoice == '1':
print(" please enter a password. ")
UserPassword = input("")
if len(UserPassword) <= 8 or len(UserPassword) >= 24 or UserPassword == UserPassword.isupper() or UserPassword == UserPassword.isdigit() or UserPassword == UserPassword.islower() or UserPassword == UserPassword.isalpha():
print("make sure your password includes numbers and upper and lower case letters ")
UserPassword = input('please enter a new password')
else:
print('your password is a very good one that is difficult to crack')
return Menu()
Notes to future readers:
Please do not write code in the form above:
while True:
optionchoice = menu()
The above code would have been better in this form:
import time
def menu():
print(" press 1 for password checker ")
print(" press 2 for password generator")
print(" press 3 to Quit")
return input("which option would you like to choose?")
def checkpasswordandinput():
while True:
optionchoice = menu()
if optionchoice == '1':
#Enter password and validate
pass
# detect other options
if optionchoice == '3':
break
checkpasswordandinput()
Upvotes: 0
Views: 328
Reputation: 82939
Those checks in the form UserPassword == UserPassword.isupper()
will not work. Here, you are comparing a string, the password, to a boolean, the result of isX()
. Hence, all those checks are False
and you get to the else
branch (if the length is acceptable).
In the case of upper and lowercase chars, you could use UserPassword == UserPassword.upper()
instead (just upper
, not isupper
, and analogously with lower()
), i.e. compare the password to it's upper/lowercased version, but for digits this does not work. Instead, you can use any
to check if any char is a digit: any(c.isdigit() for c in UserPassword)
Edit: You can use UserPassword == UserPassword.upper()
to check if the password does not contains any lowercase letters, which is kind of unintuitive. Instead, I'd suggest using any
for all the checks and also inversing the condition so the "positive" case is in the if
s body and the "negative" in the else. Something like this:
up = UserPassword
if 8 <= len(up) <= 24 and any(c.isupper() for c in up) and any(c.islower() for c in up) and any(c.isdigit() for c in up) and any(c.isalpha() for c in up):
Or a bit shorter, using a list of functions:
if 8 <= len(up) <= 24 and all(any(f(c) for c in up) for f in (str.islower, str.isupper, str.isdigit, str.isalpha)):
Upvotes: 1
Reputation: 1
So you need to do something like the following:
hasUpper = False
hasLower = False
hasDigit = False
etc etc
Then, go through the 'password' one character at a time: ie. string[1], string[2], string[3]
Run your boolean result on it (isupper, islower, etc) when true, change your hasUpper, hasLower, hasDigit to True
Then, once gone through the whole string, check your boolean results with your requirements. ie:
if requirements are upper, lower, and digit.
if hasUpper = True and hasLower = True and hasDigit = True:
then it's a good password, etc.
Make sense?
Upvotes: 0