Start learning free

LESSON 169 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 - Scored 0-100 in Python

Practice

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

password = "Secret!99"

Build:

Length: 9
Score: 100

Rules:

  • Collect the output lines in a variable named receipt: one string with the lines joined by "\n" (for example build a list lines and join it). The grader prints receipt for you, so do not call print.
  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Add the line "Length: " followed by the length, then add the line "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Same character-class detection as v3, then five independent if statements that add to score.

password = "Secret!99"

length = len(password)
has_upper = False
has_lower = False
has_digit = False
has_symbol = False
for c in password:
    if c.isupper():
        has_upper = True
    if c.islower():
        has_lower = True
    if c.isdigit():
        has_digit = True
    if not c.isalnum():
        has_symbol = True
score = 0
if length >= 8:
    score += 25
if has_upper:
    score += 20
if has_lower:
    score += 20
if has_digit:
    score += 20
if has_symbol:
    score += 15
lines = []
lines.append("Length: " + str(length))
lines.append("Score: " + str(score))
receipt = "\n".join(lines)

STEP 1 OF 20
    Ctrl + Enter to check

    Password Strength V6 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free