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 listlinesand join it). The grader printsreceiptfor you, so do not callprint. - 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)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
