Writing in capital letters feels like shouting. A moderator bot flags such messages.
What to build
A program that finds messages written in all capital letters.
Requirements
- A message is shouting if it has at least 5 letters (characters that are letters, ignoring digits, spaces and punctuation) and every letter is upper case.
- Print
N: shoutingorN: okfor each message, numbering from 1. - Finally print
COUNT shouting.
Input
The first line has n (1 to 50). Each of the next n lines is a message.
Output
n lines, then the count line.
Constraints
1 ≤ n ≤ 50.
Example
Input:
3
Thanks for the fix!
WHY IS THIS STILL BROKEN
OK
Output:
1: ok
2: shouting
3: ok
1 shouting
Why it works
Only letters have a case, so the program first keeps the letters and then tests them all; the minimum of five letters stops short words such as OK or PR from being flagged.
Examples
3 Thanks for the fix! WHY IS THIS STILL BROKEN OK
1: ok 2: shouting 3: ok 1 shouting
2 Could you share the log? please merge
1: ok 2: ok 0 shouting
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Collect the letters of the message with a list comprehension: [c for c in text if c.isalpha()].
Hint 2
It is shouting when there are at least 5 letters and all of them are upper case.
Hint 3
Count the shouting messages for the last line.
Solution
Show one correct solution (try it yourself first)
n = int(input())
count = 0
for number in range(1, n + 1):
text = input()
letters = [c for c in text if c.isalpha()]
if len(letters) >= 5 and all(c.isupper() for c in letters):
count += 1
print(f"{number}: shouting")
else:
print(f"{number}: ok")
print(f"{count} shouting")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step