I'm working on an exercise problem from the regex chapter in 'Automate the boring stuff with Python' the question and my code is below. I got the regex working in the Python shell and I think its correct but I just can't get it to work within my function so that it returns the correct answer.
Write a function that uses regular expressions to make sure the password string it is passed is strong. A strong password is defined as one that is at least eight characters long, contains both uppercase and lowercase characters, and has at least one digit. You may need to test the string against multiple regex patterns to validate its strength.
import re
def pwRegex():
print "Please enter a password"
user_pw = raw_input("> ")
#your_pw = str(user_pw)
passGex = re.compile(r'^(?=.*\d)(?=.*[A-Z])\w{8,15}$')
pass_w = passGex.search(user_pw)
if pass_w != '':
print "Well done"
else:
print "Try again"
pwRegex()
the output I get is "Try again" every time even when I'm entering a password that should pass the regex. I tried making the if statement pass_w == True: but then everything that I entered seemed to pass the regex even if incorrect.
Regex searching in python gives back a MatchObject, not a string. If it does not find the required pattern, it will return None. You should check for None, or, more pythonically, check for truthiness
# if pass_w != None: # => less pythonic
if pass_w:
print 'Well done'
else:
print 'Try again'
import re
print('Please set a new password: ')
def strongpassword():
while True:
password = input()
if lowcase.search(password) == None:
print('The entered password doesn\'t have a lower case character')
continue
if upcase.search(password) == None:
print('The entered password doesn\'t have an upper case character')
continue
if digit.search(password) == None:
print('The entered password doesn\'t have a digit')
continue
if space_8.search(password) == None:
print('The entered password should have atleast 8 characters and no space in between')
continue
else:
print('New Password is Valid and Saved')
break
lowcase = re.compile(r'[a-z]') # this regex searches for atleast one lower case alphabet
upcase = re.compile(r'[A-Z]') # this regex searches for atleast one upper case alphabet
digit = re.compile(r'(\d)') # this regex searches for atleast one digit
space_8 = re.compile(r'^[a-zA-Z0-9]{8,}$') # this regex searches for expressions without any space and atleast 8 characters
strongpassword()
I guess, it is not relevant anymore. However, I would like to suggest this:
import re
pasword = re.compile(r'''(
\d+ #have at lest one digit
.* # anything
[A-Z]+ # at least 1 capital
.*
[a-z]+ # at least one lower
.*
)''', re.VERBOSE)
# test
pasword.search('1##A!a').group()
def is_strong_password():
pas = input('Enter a password:')
if len(pas) < 8:
return False
else:
check = pasword.search(''.join(sorted(pas))).group() #sorted is important to be sure that user can write symbols in any order he/she wants
if (not check):
print('Not strong pasword')
return False
else:
print('Strong password')
return True
I was also working on the same exercise, I'm learning python as well. You can search() instead of findall(). i just used it just to test it out.
import re
print('Enter new password')
def strongPW():
while True:
pw = input()
lowercase = [] # lowercase
uppercase = [] # uppercase
digit = [] # number
if len(pw) < 8:
print('your password must have 8 characters or more')
break
# lowercase verification
pwRegex = re.compile(r'[a-z]')
lowercase = pwRegex.search(pw)
if lowercase == []:
print('Your password must have at least one lowercase')
break
# uppercase verification
uppercaseRegex = re.compile(r'[A-Z]')
uppercase = uppercaseRegex.findall(pw)
if uppercase == []:
print('Your password must have at least one uppercase')
break
# digit verification
DigitRegex = re.compile(r'[0-9]')
digit = DigitRegex.findall(pw)
if digit == []:
print('Your password must have at least one digit')
break
else:
print('Your password meets all requirements ')
break
strongPW()
Related
I make password genrator using python then the teacher told to create a code that make the user choose what he want in the password
the password contains
1- lower case characters
2- upper case characters
3- numbers
4- punticuation marks
the teacher want me to make the user choose if he want punticution or not
if he dont want it have to be deleted from the password
i tried so hard but i got stuck
from msilib import change_sequence
import string
import random # I import this line to make the password unorgnaized
# this code below is all the characters we need for the password
s1 = list(string.ascii_lowercase)
s2 = list(string.ascii_uppercase)
s3 = list(string.digits)
s4 = list(string.punctuation)
# this code is for the user to put how much characters he need
characters_number = input("how many characters for the password?:")
# this while loop code is to make the user choose only 6 characters and up
while True:
try:
characters_number = int(characters_number)
if characters_number < 6 :
print("you need at least 6 characters") # if the user choose an letter or digits it will not allow him
characters_number = input("please enter the number again:")
else:
break # I break the loop here if the user write the correct data
except: #this code here if the user enter anything except numbers
print("please enter numbers only")
characters_number = input("how many characters for the password?:")
# the random and shuffle is to make the password unorgnized
random.shuffle(s1)
random.shuffle(s2)
random.shuffle(s3)
random.shuffle(s4)
# the password that will appear to the user it contains upper and lower letters, numbers and digit
# part1 here is for the letters I allocated 30% of the password for letters
# part2 here is for the digits and numbers I allocated 20% of the password for letters
part1 = round(characters_number * (30/100))
part2 = round(characters_number * (20/100))
password = []
for x in range(part1):
password.append(s1[x])
password.append(s2[x])
#the for loops here here is to create the password
for x in range(part2):
password.append(s3[x])
password.append(s4[x])
#this code here is to transform the password for list method to a string
password = "".join(password[0:])
print(password)
You can write a function like this:
def isUserNeedPunctuationMarks():
result = None
while True:
inp = input("Do you need punctuation marks in your password? (y/n)")
inp = inp.lower()
if inp == "y":
result = True
break
elif inp == "n":
result == False
break
else:
print("You must enter y or n")
return result
The above function will return True if the user needs punctuation marks, and False otherwise. User has to type y (for yes) or n(for no).
You can call isUserNeedPunctuationMarks() in the right place in your code. For example, removing punctuation marks from password string, you have to write a condition like this:
import re
if not isUserNeedPunctuationMarks():
password = re.sub(r'[^\w\s]', '', password) # This removes punctuation marks from a string using regex
Hope this helps :)
Task: Password validator
a. The length of the password needs to be from 8 to 30 characters.
b. The password needs to contain at least 2 uppercase characters.
c. The password needs to contain at least 2 lowercase characters.
d. The password needs to contain at least 1 digit.
e. The password needs to contain exactly 1, but not more of the following special characters:
#/%&*_-
This is my code:
special_character_set = "#/%&*_-"
is_valid = False
import re
while True:
password = input("please enter a password: ")
if len(password) > 30 or len(password)< 8:
print("The length of the password needs to be from 8 to 30 characters!")
continue
elif re.search('[0-9]', password) is None:
print("The password needs to contain at least 1 digit!")
continue
elif re.search ('[A-Z][A-Z]', password) is None:
print("The password needs to contain at least 2 uppercase characters!")
continue
elif re.search('[a-z][a-z]', password) is None:
print("The password needs to contain at least 2 lowercase characters!")
continue
elif re.search('[#/%&*_-]', password) is None:
print("The password needs to contain one of the following special characters: #/%&*_-!")
continue
else:
is_valid = True
if is_valid:
print("valid password")
break
It works. However, the condition e) has not been completely fulfilled. How can I make the password contain ONLY ONE of the special characters?
What about len + re.findall():
...
elif len(re.findall('[#/%&*_-]', password)) != 1:
# Some number of special characters other than 1 was found
...
or
...
elif len(re.findall('[#/%&*_-]', password)) == 0:
# No special characters were found
...
elif len(re.findall('[#/%&*_-]', password)) > 1:
# Too many special characters were found
(and if you do the latter, try not to run the regex twice)
Don't use regex for this; add up the counts of special characters in the proposed password:
>>> passwd = "My_Pass#"
>>> special = '[#/%&*_-]'
>>> special_count = sum(passwd.count(c) for c in special)
>>> special_count
2
You can now use special_count as desired.
I'm working on a password checker that checks if the string is a valid password. I have to check if there is at least eight characters, must consist of only letters and digits and the last two characters must be digits.
It all seems to work so far other than the password.isdigit(). sometimes the password comes out valid and sometimes it doesn't. Any suggestions?
# Gets the users password
password = input('Enter a string for password: ')
# Splices the last two characters of the password
lastTwo = password[-2:]
# Checks the password if it is less than 8 characters
while len(password) < 8:
print('The password you entered is too short.')
print()
password = input('Enter a string for password: ')
# Checks the password if it is composed of letters and numbers
while password.isalnum() == False:
print('Your password has special characters not allowed.')
print()
password = input('Enter a string for password: ')
# Checks the spice to verify they are digits
while lastTwo.isdigit() == False:
print('Your last two characters of your password must be digits.')
print()
password = input('Enter a string for password: ')
print('Your password is valid.')
There are a handful of issues with your provided code. Particularly, you only check the subsequent rules while len(password) < 8. If you give it a password of length 10, the rules are never checked. Additionally, you don't update the lastTwo with each new password attempted
One way to fix this would be to replace your several while statements with if...elif..elif...else... wrapped in an overall while statement, as follows:
# Gets the users password
password = input('Enter a string for password: ')
while True:
# Checks the password if it is less than 8 characters
if len(password) < 8:
print('The password you entered is too short.')
# Checks the password if it is composed of letters and numbers
elif not password.isalnum():
print('Your password has special characters not allowed.')
# Checks the spice to verify they are digits
elif not password[:-2].isdigit():
print('Your last two characters of your password must be digits.')
else:
# we only get here when all rules are True
break
print()
password = input('Enter a string for password: ')
print('Your password is valid.')
This should work as you intended it. But while we're at it, why not tell the user every rule their password has broken? From a UI point of view, it helps to keep the user informed.
If we store an information message alongside whether the relevant rule has been met, we can quickly work out all of the rules that have been broken, like so:
valid_password = False
while not valid_password:
# Get a password
password = input('\nEnter a string for password: ')
# applies all checks
checks = {
'- end in two digits': password[-2].isdigit(),
'- not contain any special characters': password.isalnum(),
'- be over 8 characters long': len(password) > 8
}
# if all values in the dictionary are true, the password is valid.
if all(checks.values()):
valid_password = True
# otherwise, return the rules violated
else:
print('This password is not valid. Passwords must:\n{}'.format(
'\n'.join([k for k, v in checks.items() if not v])))
print('Your password is valid.')
You never update your value of lastTwo inside your while loop. Thus imagine if a user first entered a password abc123. Then lastTwo would be calculated as 23.
Now your code would find that the password is too short and prompt the user for a new password. Suppose he enters abcdefgh. This now passes your first and second checks. Notice however that lastTwo is still 23, and thus your third check will incorrectly pass.
You should thus recalculate the value of lastTwo whenever you accept a new password or directly check like this:
while (password[-2:]).isdigit() == False:
As part of an assignment, I'm creating a program to ask a user to input a username and password.
I've completed the username part (easy), but in the assignment instructions, the password must:
contain an uppercase character
contain a lowercase character
be longer than 6 characters
contain a number
have no white space
There are 5 specific error messages that need to display if any of these conditions are not true.
What I've got currently has some weird behavior, the errors only show up in a specific order (for example, password "f" will have the error "must have an uppercase character" instead of "must be longer than 6 characters"). I know this is because of the nesting order of the if statements, but if there's a better way... It also for some reason doesn't catch white space in the password. I know it probably isn't the most efficient way but here's what I've got so far:
def validatePassword():
accept = "n"
while accept == "n":
password = input("Please enter a valid password: ")
upper = "n"
lower = "n"
digit = "n"
length = "n"
white = "n"
if len(password) >= 6:
length = "y"
for char in password:
if char in string.ascii_uppercase:
upper = "y"
if char in string.ascii_lowercase:
lower = "y"
if char in string.digits:
digit = "y"
if char not in string.whitespace:
white = "y"
if upper == "y":
if lower == "y":
if digit == "y":
if length == "y":
if white == "y":
accept = "y"
else:
print("::: ERROR :::",password,"must not have any white space character.")
else:
print("::: ERROR :::",password,"must be longer than 6 characters.")
else:
print("::: ERROR :::",password,"must contain a number.")
else:
print("::: ERROR :::",password,"must contain a lowercase character.")
else:
print("::: ERROR :::",password,"must contain an uppercase character.")
return password
You could try something like this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def validatePassword(password):
if not any(x.isupper() for x in password):
return False, "No upper case character"
if not any(x.islower() for x in password):
return False, "No lower case character"
if len(password) < 6:
return False, "Not longer than 6 characters"
if not any(x.isdigit() for x in password):
return False, "No number"
if any(x.isspace() for x in password):
return False, "Contains hitespace"
return True, ""
def test():
# test some passwords
test_passwords = ["lkjasdf", "KAJSDF", "lfSF", "asdfADSF", "asdf ADSF 123", "asdfADSF123"]
for p in test_passwords:
print validatePassword(p)
# "real" usage
while True:
password = input("Please enter a valid password: ")
result, message = validatePassword(password)
if result is True:
break
else:
print(message)
if __name__ == '__main__':
test()
Obviously the correct way to do this is with any() and a generator expression, but seeing as how you said I'm like 8 weeks into a 10 week introductory course those probably don't help you.
Here's how you can do it without using any() by just looping through the characters checking for the presence of certain types of characters.
def validate_password(password):
hasUpper = hasLower = hasNumber = hasWhiteSpace = False
for char in passwordStr:
if char.isupper():
hasUpper = True
if char.islower():
hasLower = True
if char.isdigit():
hasNumber = True
if char.isspace():
hasWhiteSpace = True
if len(password) <= 6:
return False, "Password must be longer than 6 characters"
if not hasUpper:
return False, "Password must contain atleast one uppercase character"
if not hasLower:
return False, "Password must contain atleast one lowercase letter"
if not hasNumber:
return False, "Password must contain alteast one number"
if hasWhiteSpace:
return False, "Password cannot contain any white space"
return True
First, there's the data type bool which can represent the values True and False. You should use them to represent simple true/false states instead of assigning the strings "y" and "n".
Second, checking every character separately and storing whether the different conditions are met at least once is a possible way. However, Python has powerful string examination methods that can make your life much easier. Let me demonstrate them by writing a short function which takes aa password as argument and simply returns True or False depending on whether the passwords meets your restrictions:
def is_password_valid(password):
# check password length:
if len(password) < 6:
print("Your password is too short, at least 6 characters are required!")
return False
# check if password contains any non-alphanumeric letters (including whitespace):
# (isalnum() only returns true if the string only contains letters and digits, nothing else)
if not password.isalnum():
print("Your password may only contain letters and digits!")
return False
# check if password contains letters:
# (isdigit() only returns true if all of the characters are numbers)
if password.isdigit():
print("Your password does not contain any letters!")
return False
# check if password contains uppercase letters:
# (islower() only returns true if all of the letters are lowercase, non-letters are ignored)
if password.islower():
print("Your password does not contain any uppercase letters!")
return False
# check if password contains lowercase letters:
# (isupper() only returns true if all of the letters are uppercase, non-letters are ignored)
if password.isupper():
print("Your password does not contain any lowercase letters!")
return False
# check if password contains lowercase numbers:
# (isalpha() only returns true if all of the characters are letters)
if password.isalpha():
print("Your password does not contain any numbers!")
# if all conditions above are okay, the password is valid:
print("The password is okay.")
return True
You may of course reorder the blocks in my code above as you wish.
You can then simply use this helper function inside your loop which looks a lot simpler and cleaner now:
def validate_password():
accept = False
while not accept:
password = input("Please enter a valid password: ")
accept = is_password_valid(password)
You can try Regular Expressions. If you don't know about regex, as you've mentioned in your comment, this is a great website.
For validation, check if the password matches with any of the rules which will render it invalid and if not, it is a valid password.
import re
def validate(passwd):
pattern = r'^(.{0,5}|[^0-9]*|[^A-Z]*|[^a-z]*|.*\s+.*)$'
result = re.search(pattern, passwd)
if result:
print 'Invalid Password'
else:
print 'Valid Password'
What this does is check if, passwd is less than 6 chars OR contains no numbers OR contains no uppercase chars OR contains no lowercase chars OR contains spaces. If none of the criteria matches, the password is valid.
OK so what I need to do is make my code only allow the user to enter one letter and then one symbol at a time. The example below shows what I want in a better view.
At the moment my code allows the user to enter more than one character at a time which I don't want.
What letter would you like to add? hello
What symbol would you like to pair with hello
The pairing has been added
['A#', 'M*', 'N', 'HELLOhello']
What I want is a message to be displayed like this and the pairing not to be added to the list.
What letter would you like to add? hello
What symbol would you like to pair with hello
You have entered more than one character, the pairing was not added
['A#', 'M*', 'N',].
So far my code for this section is as follows...
It would also be great for when the user enters a number in the letter section, an error message to be printed.
def add_pairing(clues):
addClue = False
letter=input("What letter would you like to add? ").upper()
symbol=input("\nWhat symbol would you like to pair with ")
userInput= letter + symbol
if userInput in clues:
print("The letter either doesn't exist or has already been entered ")
elif len(userInput) ==1:
print("You can only enter one character")
else:
newClue = letter + symbol
addClue = True
if addClue == True:
clues.append(newClue)
print("The pairing has been added")
print (clues)
return clues
The easiest way to ensure user input is with a loop:
while True:
something = raw_input(prompt)
if condition: break
Something set up like this will continue to ask prompt until condition is met. You can make condition anything you want to test for, so for you, it would be len(something) != 1
Your method can be simplified to the following if you let the user enter a letter and symbol pair:
def add_pairing(clues):
pairing = input("Please enter your letter and symbol pairs, separated by a space: ")
clues = pairing.upper().split()
print('Your pairings are: {}'.format(clues))
return clues
Not exactly sure what you want to return but this will check all the entries:
def add_pairing(clues):
addClue = False
while True:
inp = input("Enter a letter followed by a symbol, separated by a space? ").upper().split()
if len(inp) != 2: # make sure we only have two entries
print ("Incorrect amount of characters")
continue
if not inp[0].isalpha() or len(inp[0]) > 1: # must be a letter and have a length of 1
print ("Invalid letter input")
continue
if inp[1].isalpha() or inp[1].isdigit(): # must be anything except a digit of a letter
print ("Invalid character input")
continue
userInput = inp[0] + inp[1] # all good add letter to symbol
if userInput in clues:
print("The letter either doesn't exist or has already been entered ")
else:
newClue = userInput
addClue = True
if addClue:
clues.append(newClue)
print("The pairing has been added")
print (clues)
return clues
I am fan of raising and catching exceptions in similar cases. Might be shocking for people with 'C-ish' background (sloooow), but it is perfectly pythonic and quite readable and flexibile in my opinion.
Also, you should add check for characters outside of set you are expecting:
import string
def read_paring():
letters = string.ascii_uppercase
symbols = '*##$%^&*' # whatever you want to allow
letter = input("What letter would you like to add? ").upper()
if (len(letter) != 1) or (letter not in letters):
raise ValueError("Only a single letter is allowed")
msg = "What symbol would you like to pair with '{}'? ".format(letter)
symbol = input(msg).upper()
if (len(symbol) != 1) or (symbol not in symbols):
raise ValueError("Only one of '{}' is allowed".format(symbols))
return (letter, symbol)
def add_pairing(clues):
while True:
try:
letter, symbol = read_paring()
new_clue = letter + symbol
if new_clue in clues:
raise ValueError("This pairing already exists")
break # everything is ok
except ValueError as err:
print(err.message)
print("Try again:")
continue
# do whatever you want with letter and symbol
clues.append(new_clue)
print(new_clue)
return clues