Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Password Checker File: password_checker.py Download the starter code (complete with hints in the form of TODO comments) from password_checker.py a program that asks for and

Password Checker

File: password_checker.py

Download the starter code (complete with hints in the form of TODO comments) from password_checker.py

a program that asks for and validates a person's password. The program is not for comparing a password to a known password, but validating the 'strength' of a new password, like you see on websites: enter your password, and then it tells you if it's valid (matches the required pattern) and re-prompts if it's not.
All passwords must contain at least one each of:

digit
lowercase letter
uppercase letter
The starter code uses constants (variables at the top of the code, named in ALL_CAPS) to store:

a. the minimum and maximum length of the password

b. whether a special character (not alphabetical or numerical) is required

Remember when a program has CONSTANTS, you should use them everywhere you can so that if you change them at the top, this change affects the whole program as expected.
E.g., if you changed the minimum length to 5, the program should print 5 and should check to make sure the password is > = 5 characters long.

Output should look something like this:

Please enter a valid password
Your password must be between 5 and 15 characters, and contain:
    1 or more uppercase characters
    1 or more lowercase characters
    1 or more numbers
    and 1 or more special characters:  !@#$%^&*()_-=+`~,./'[]<>?{}|\
> this?
Invalid password!
> whyNot?CanIhaveThis?
Invalid password!
> 12345678901234567890aBcv@
Invalid password!
> thisISmy123Pass!
Invalid password!
> 1thisISit!
Your 10 character password is valid: 1thisISit!
Here's another run with the same code but different values for the constants (special characters are not required in this version):

Please enter a valid password
Your password must be between 2 and 6 characters, and contain:
    1 or more uppercase characters
    1 or more lowercase characters
    1 or more numbers
> aB
Invalid password!
> HowCanIHave2Chars?
Invalid password!
> 1aB
Your 3 character password is valid: 1aB
Important Note: Do not just try and Google "Python password checker" or something, but think about  this step by step. We have started this for you with TODO comments in the code provided. Follow these.

First, just check if a string has at least one lowercase character.
You can do this by looping through the string (for character in password:) and testing each character... count the ones that match (using character.islower())... At the end you know how many lowercase characters there are.

Only when you are able to count lowercase, then, in the same loop, count the uppercase characters
That is, do not try and get all the checks working before you know the first one works. Do one at a time.

Then, count the numbers...
Test your code for each of these changes as you write them.

For special characters, remember you can use the in operator to see if the character is in another string (like a constant called SPECIAL_CHARACTERS)

... keep going until you can tell how many of each kind of character there are.

Then put it all together and test with some different settings.

We hope this incremental approach makes sense and that you use it regularly.

When you have the program working, replace the inconsistent printing of text and variables with nice string formatting using f-strings.

 

"""
CP1404/CP5632 - Practical
Password checker "skeleton" code to help you get started
"""

MIN_LENGTH = 2
MAX_LENGTH = 6
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "..!@#$%^&*()_-=+`~,./'[]<>?{}|\\"


def main():
   """Program to get and check a user's password."""
   print("Please enter a valid password")
   print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
         "characters, and contain:")
   print("\t1 or more uppercase characters")
   print("\t1 or more lowercase characters")
   print("\t1 or more numbers")
   if SPECIAL_CHARS_REQUIRED:
       print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
   password = input("> ")
   while not is_valid_password(password):
       print("Invalid password!")
       password = input("> ")
   print(f"Your {len(password)}-character password is valid: {password}")


def is_valid_password(password):
   """Determine if the provided password is valid."""
   # TODO: if length is wrong, return False

   count_lower = 0
   count_upper = 0
   count_digit = 0
   count_special = 0
   for char in password:
       # TODO: count each kind of character (use str methods like isdigit)
       pass

   # TODO: if any of the 'normal' counts are zero, return False

   # TODO: if special characters are required, then check the count of those
   # and return False if it's zero

   # if we get here (without returning False), then the password must be valid
   return True


main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Concepts Late Objects

Authors: Cay S. Horstmann

3rd Edition

1119186714, 978-1119186717

Students also viewed these Programming questions

Question

a sin(2x) x Let f(x)=2x+1 In(be)

Answered: 1 week ago