Python Forum

Full Version: Regex Issue from Automate the Boring Stuff book
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the "Automate the Boring Stuff with Python" book by Al Sweigart and have been working through the book the last week and I'm currently on Chapter 7 on regular expressions. I'm trying to do the first Practice Project at the end of the chapter whose objective is to create a function that uses regular expressions to make sure a password string that it is passed is strong. Its definition of a strong password is it must: be at least 8 characters long; contain at least one uppercase; contain at least one lowercase; and contain at least one digit.

I've wrote up some code here but it fails when I run it on the command line after I enter a purposely weak password:


#!/usr/bin/env python3

import re

def strongPass(text):
	passLength = re.compile(r'(\w){7}(\w)+')
	passUpper  = re.compile(r'[A-Z]+')
	passLower  = re.compile(r'[a-z]+')
	passNumber = re.compile(r'[0-9]+') 
	
	lengthResult = passLength.findall(text)
	upperResult  = passUpper.search(text)
	lowerResult  = passLower.search(text)
	numberResult = passNumber.search(text)
	
	if lengthResult.group() == None:
		print("Password must be at least 8 characters long.")
	elif upperResult.group() == None:
		print("Password must contain an uppercase.")
	elif lowerResult.group() == None:
		print("Password must contain a lowercase.")
	elif numberResult == None:
		print("Password must contain a number.")
	else:
		print("Password accepted!")

print("Please enter a password:")
teststring = input()
strongPass(teststring)
The output of running it and entering a weak password:
Error:
Please enter a password: poop Traceback (most recent call last): File "passTest.py", line 29, in <module> strongPass(teststring) File "passTest.py", line 16, in strongPass if lengthResult.group() == None: AttributeError: 'list' object has no attribute 'group'
#!/usr/bin/env python3
 
import re
 
def strongPass(text):
    passLength = re.compile(r'\w{8,}')
    passUpper  = re.compile(r'[A-Z]+')
    passLower  = re.compile(r'[a-z]+')
    passNumber = re.compile(r'[0-9]+') 
     
    lengthResult = passLength.findall(text)
    upperResult  = passUpper.search(text)
    lowerResult  = passLower.search(text)
    numberResult = passNumber.search(text)
     
    if lengthResult[0] == None:
        print("Password must be at least 8 characters long.")
    elif upperResult == None:
        print("Password must contain an uppercase.")
    elif lowerResult == None:
        print("Password must contain a lowercase.")
    elif numberResult == None:
        print("Password must contain a number.")
    else:
        print("Password accepted!")
 
print("Please enter a password:")
teststring = input()
strongPass(teststring)
FYI to anyone who doesn't understand what happened in this thread, metulburr is my brother and I was at his house and we were trying to hash this out and fix it. However the code metulburr posted will fail if the input is shorter than 8 characters, my revised code:

#!/usr/bin/env python3

import re

def strongPass(text):
	passLength = re.compile(r'(\w){8,}')
	passUpper  = re.compile(r'[A-Z]+')
	passLower  = re.compile(r'[a-z]+')
	passNumber = re.compile(r'[0-9]+') 
	
	lengthResult = passLength.search(text)
	upperResult  = passUpper.search(text)
	lowerResult  = passLower.search(text)
	numberResult = passNumber.search(text)

	if lengthResult == None:
		print("Password must be at least 8 characters long.")
	elif upperResult == None:
		print("Password must contain an uppercase.")
	elif lowerResult == None:
		print("Password must contain a lowercase.")
	elif numberResult == None:
		print("Password must contain a number.")
	else:
		print("Password accepted!")

print("Please enter a password:")
teststring = input()
strongPass(teststring)