Start learning free

LESSON 165 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 - Has Digit in Python

Practice

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

password = "Secret99"

Build:

Length: 8
Has digit: True
Strong

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.
  • Add the line "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag has_digit, which starts as False and becomes True when a digit is seen.
  • Add the line "Has digit: " followed by the flag.
  • If the length is at least 8 and has_digit is True, add the line Strong; otherwise add the line Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

Use c.isdigit() in a loop to detect digits.

password = "Secret99"

length = len(password)
lines = []
lines.append("Length: " + str(length))
has_digit = False
for c in password:
    if c.isdigit():
        has_digit = True
lines.append("Has digit: " + str(has_digit))
if length >= 8 and has_digit:
    lines.append("Strong")
else:
    lines.append("Weak")
receipt = "\n".join(lines)

STEP 1 OF 10
    Ctrl + Enter to check

    Password Strength V2 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