ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
building.py 1.1 KB
import sys
import string
def count_characters(text: str) -> None:
"""Print how many characters of each category text holds."""
upper = sum(1 for c in text if c.isupper())
lower = sum(1 for c in text if c.islower())
punct = sum(1 for c in text if c in string.punctuation)
space = sum(1 for c in text if c.isspace())
digit = sum(1 for c in text if c.isdigit())
print(f"The text contains {len(text)} characters:")
print(f"{upper} upper letters")
print(f"{lower} lower letters")
print(f"{punct} punctuation marks")
print(f"{space} spaces")
print(f"{digit} digits")
def main() -> None:
"""Read the text from the argument or from stdin, then count it."""
try:
if len(sys.argv) > 2:
raise AssertionError("more than one argument is provided")
if len(sys.argv) == 2:
text = sys.argv[1]
else:
print("What is the text to count?")
text = sys.stdin.readline()
count_characters(text)
except AssertionError as error:
print(f"AssertionError: {error}")
if __name__ == "__main__":
main()