My daily exercise today is a small word counter. Had to look up .split(), the rest I knew π
# This Program counts the number of words in a string
import logging
import sys
# Configure logging
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s: %(message)s')
def count_words(text):
words = text.split()
number = 0
for _ in words:
number += 1
return number
def main():
while True:
try:
user_input = input('Input: ')
if user_input == "":
logging.error('Please type something')
else:
break
except KeyboardInterrupt:
sys.exit(' Exiting Program')
number_of_words = count_words(user_input)
print(f'Number of words: {number_of_words}')
if __name__ == '__main__':
main()
Top comments (0)