Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?
You start with:
password = "Secret!99"
Build:
Length: 9 Upper: True Lower: True Digit: True Symbol: 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. - Start four flags (
has_upper,has_lower,has_digit,has_symbol) atFalse. Loop over every character and set the matching flag toTrue. - A symbol is any character that is not a letter and not a digit.
- Add the line one line per flag, in this order:
"Upper: ","Lower: ","Digit: ","Symbol: ", each followed by the flag. - If the length is at least 8 and all four flags are
True(stricter than the last lesson), add the lineStrong; otherwise add the lineWeak.
Try other passwords: with secret99 the output is
Length: 8 Upper: False Lower: True Digit: True Symbol: False Weak
c.isupper(), c.islower(), c.isdigit(), and not c.isalnum() cover the four classes.
password = "Secret!99"
length = len(password)
lines = []
lines.append("Length: " + str(length))
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
lines.append("Upper: " + str(has_upper))
lines.append("Lower: " + str(has_lower))
lines.append("Digit: " + str(has_digit))
lines.append("Symbol: " + str(has_symbol))
if length >= 8 and has_upper and has_lower and has_digit and has_symbol:
lines.append("Strong")
else:
lines.append("Weak")
receipt = "\n".join(lines)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
