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 listlinesand join it). The grader printsreceiptfor you, so do not callprint. - Add the line
"Length: "followed by the number of characters. - Loop through each character to set a boolean flag
has_digit, which starts asFalseand becomesTruewhen a digit is seen. - Add the line
"Has digit: "followed by the flag. - If the length is at least 8 and
has_digitisTrue, add the lineStrong; otherwise add the lineWeak.
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)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
