-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_stats.py
31 lines (26 loc) · 1000 Bytes
/
text_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def main():
# Prompt the user to input a sentence
sentence = input("Enter a sentence: ")
# Initialize counters for words, characters, digits, and spaces
words = 1 # We start at 1 because we distinguish words by spaces, and the first word doesn't have a space before it
characters = 0
digits = 0
spaces = 0
# Iterate through each character in the sentence
for char in sentence:
if char == " ":
words += 1 # Count spaces to determine the number of words
spaces += 1
elif char.isdigit():
digits += 1 # Count digits
characters += 1 # Count characters (including digits)
elif char.isalpha():
characters += 1 # Count characters (excluding digits)
# Print the analysis results
print("The text contains:")
print(f"{words} words")
print(f"{characters} characters")
print(f"{digits} digits")
print(f"{spaces} spaces")
if __name__ == "__main__":
main()