Python Forum
Regex Issue from Automate the Boring Stuff book
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex Issue from Automate the Boring Stuff book
#1
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'
Reply
#2
#!/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)
Recommended Tutorials:
Reply
#3
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)	
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  issue in using regex akbarza 4 509 Nov-14-2023, 10:00 AM
Last Post: buran
  Facing issue in python regex newline match Shr 6 1,147 Oct-25-2023, 09:42 AM
Last Post: Shr
  automate new PDF creation with Bookmarks Based up Regex standenman 0 1,121 Jan-16-2023, 10:56 PM
Last Post: standenman
  Run the code for some stuff it does not return me why Anldra12 3 2,798 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,822 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 2,280 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,133 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,958 Apr-29-2020, 05:07 PM
Last Post: bako
  Getting Cells from the Sheets "automate the boring stuff" Shafla 8 3,929 Sep-24-2019, 04:53 AM
Last Post: snippsat
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,079 Jul-02-2019, 02:29 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020