Count how many characters of each class the password contains.
You start with:
password = "Hello!World9"
Build:
Uppercase: 2 Lowercase: 8 Digits: 1 Symbols: 1
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. - Use four integer counters, all starting at 0.
- Loop through each character and add 1 to the matching counter.
- Treat any character that is not a letter and not a digit as a symbol.
- Add the line one line per counter, in this order:
"Uppercase: ","Lowercase: ","Digits: ","Symbols: ", each followed by the count.
Try other passwords: with Pass word!2 the output is
Uppercase: 1 Lowercase: 7 Digits: 1 Symbols: 2
Use
+= 1 instead of setting flags.
password = "Hello!World9"
uppercase = 0
lowercase = 0
digits = 0
symbols = 0
for c in password:
if c.isupper():
uppercase += 1
if c.islower():
lowercase += 1
if c.isdigit():
digits += 1
if not c.isalnum():
symbols += 1
lines = []
lines.append("Uppercase: " + str(uppercase))
lines.append("Lowercase: " + str(lowercase))
lines.append("Digits: " + str(digits))
lines.append("Symbols: " + str(symbols))
receipt = "\n".join(lines)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
