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:
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'
