Problem overview
Version 2 keeps the length check from version 1 and adds a second rule: a strong password must also contain at least one digit. The starting code sets password = "Secret99". The receipt, stored in receipt, has three lines: the length, whether a digit was found, and the verdict, Strong or Weak.
Length: 8
Has digit: True
Strong
For "abcdefgh" the length passes, but there is no digit, so the lines are Length: 8, Has digit: False and Weak.
Approach: a boolean flag set by a loop
To know whether any character is a digit, the program has to look at every character. The usual pattern is a flag: a boolean that starts as False ("no digit seen yet") and is set to True the moment a digit appears. A loop over the password visits each character once and tests it.
The flag only ever moves from False to True. No branch sets it back, so one digit anywhere in the password is enough, even if later characters are letters.
Step by step
- Create
length.len(password), used in the first line and in the verdict. - Create the empty list
lines. - Append the length line.
Length:followed bystr(length). - Create
has_digitasFalse. The assumption is that there is no digit until one is found. - Loop over the characters.
for c in passwordgivescone character per pass. - Test each character. When
c.isdigit()is true, sethas_digittoTrue. - Append the flag line after the loop. The line is not indented, so it runs once, when every character has been checked.
str()of a boolean gives the textTrueorFalse, which is what the output shows. - Write the verdict's
if. Whenlength >= 8 and has_digit, appendStrong. Both conditions must hold. - Write the
else. It appendsWeakfor everything else: too short, no digit, or both. - Create
receipt."\n".join(lines).
Watching the flag change
Here is the loop on the password "ab9c", with the value of has_digit printed after each character:
'a' False
'b' False
'9' True
'c' True
The flag flips at the digit and stays true when the letter c follows. The final value is what the second output line reports.
The solution in Python
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)
Python can also fold the loop and the flag into one expression. any(c.isdigit() for c in password) is True for "Secret99" and False for "abcdefgh". The explicit loop in this lesson does the same work and shows each step.
Details worth knowing:
- Looping over a string. A
forloop over a string produces its characters one at a time, and each is a one-character string. isdigit(). It returnsTruefor a one-character string such as"9"andFalsefor"a"or"!". It is a method of the string, so it is called asc.isdigit(), with parentheses.- It isn't limited to 0 to 9.
isdigit()also accepts digits from other scripts. With the Arabic-Indic digits"٣٤٥٦٧٨٩٠", the flag isTrueand the verdict isStrong. The lesson's passwords use ordinary digits. - The flag keeps its value. Because the
ifhas noelse, a letter after a digit can't reset the flag. andneeds both sides true. If either side is false, the whole condition is false and theelsebranch runs.
Complexity
Time: O(n), where n is the number of characters. The loop makes one pass, and each character gets a fixed number of constant-time tests. The line building and the verdict after the loop take the same few steps for any password.
Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.
Reading the receipt
The verdict alone doesn't say why a password is weak. The two lines above it do. Length: 4 with Has digit: True means the password was too short, while Length: 8 with Has digit: False means it had no digit. Printing the facts before the verdict makes the receipt useful as a diagnosis, and it makes each fact easy to check on its own.
Edge cases and common mistakes
- Long, but no digit. "abcdefgh" gives
Has digit: FalseandWeak. - A digit, but too short. "abc1" gives
Length: 4,Has digit: TrueandWeak. - Only digits. "12345678" is
Strongin this version, because the only rules are the length and a digit. - Resetting the flag in an
else. Addingelse: has_digit = Falseinside the loop would erase the result whenever the last character isn't a digit. - Printing the flag line inside the loop. It would be appended once per character.
- Writing
c = isdigit()orisdigit(c). The method belongs to the string:c.isdigit(). - Checking only the first or last character. A test like
password[0].isdigit()looks at one position. The loop is what makes a digit anywhere in the password count. - Forgetting
str()on the flag."Has digit: " + has_digitraises aTypeError, because a boolean is not text.
Testing
"Secret99"givesLength: 8,Has digit: TrueandStrong."abcdefgh"givesLength: 8,Has digit: FalseandWeak."abc1"givesLength: 4,Has digit: TrueandWeak."12345678"givesLength: 8,Has digit: TrueandStrong.
