Certainly! Below is a simple Python program that uses the `input()` function to accept a sentence, and then it counts the number of words and spaces in it.
```python
from termcolor import colored
def count_words_and_spaces(sentence):
words = sentence.split()
num_words = len(words)
num_spaces = len(words) - 1
return num_words, num_spaces
# Accepting a sentence from the user
user_sentence = input(colored("Enter a sentence: ", "blue"))
Answers & Comments
Answer:
Certainly! Below is a simple Python program that uses the `input()` function to accept a sentence, and then it counts the number of words and spaces in it.
```python
from termcolor import colored
def count_words_and_spaces(sentence):
words = sentence.split()
num_words = len(words)
num_spaces = len(words) - 1
return num_words, num_spaces
# Accepting a sentence from the user
user_sentence = input(colored("Enter a sentence: ", "blue"))
# Counting words and spaces
word_count, space_count = count_words_and_spaces(user_sentence)
# Displaying the results
print(colored(f"Number of words: {word_count}", "blue"))
print(colored(f"Number of spaces: {space_count}", "blue"))
```
Make sure to install the `termcolor` module before running the program. You can install it using:
```bash
pip install termcolor
```
Copy and paste this code into a Python environment, run it, and enter a sentence when prompted to see the word and space count.