In: Computer Science
#Write a function called average_word_length that takes as
#input a string called my_string, and returns as output the
#average length of the words in the string.
In writing this function, note the following:
#
# - You should account for consecutive spaces. A string like
# "Hi Lucy" is two words with an average length of 3.0
# characters.
# - You should not assume the string starts with a letter.
# A string like " David" has one word with an average
# length of 5.0 characters.
# - You should not count punctuation marks toward the
# length of a word. A string like "Hi, Lucy" has two
# words with an average length of 3.0 characters: the ,
# after "Hi" does not count as a character in the word.
# The only punctuation marks you need to handle are
# these: . , ! ?
# - You may assume the only characters in the string are
# letters, the punctuation marks listed above, and spaces.
# - If my_string is not a string, you should instead return
# the string, "Not a string".
# - If there are no words in my_string, you should instead
# return the string, "No words". This could happen for
# strings like "" (an empty string) and ".,!?" (a string
# of only punctuation marks). You may assume that any
# of these punctuation marks will always be followed by
# at least one space.
#
#Here are a few hints that might help you:
#
# - You can peak at the first character in my_string with
# my_string[0]. If my_string is "Hi, Lucy", then the value
# of my_string[0] is "H". You don't have to do this, but
# you can if you want.
# - There are lots of ways you can do this. If you're
# stuck, try taking a step back and thinking about the
# problem from a fresh perspective.
# - If you're still stuck, try counting words and letters
# separately, and worrying about average length only
# after both have been counted.
# - The word count should equal the number of letters that
# come immediately after a space or the start of the
# string. The character count should simply equal the
# number of characters besides spaces and punctuation
# marks. The average word length should be character
# count divided by word count.
#Write your function here!
average_word_length(my_string)
#When your function works, the following code should
#output:
#2.0
#3.0
#4.0
#Not a string
#No words
print(average_word_length("Hi"))
print(average_word_length("Hi, Lucy"))
print(average_word_length(" What big spaces you have!"))
print(average_word_length(True))
print(average_word_length("?!?!?! ... !"))
# do comment if any problem arises
# Code
# this function returns true if given string contains alphabets
def has_alpha(words):
for char in words:
if char.isalpha():
return True
return False
def average_word_length(words):
# if input is not a string
if not isinstance(words, str):
return "Not a string"
# if words doesnot contains alphabets
if not has_alpha(words):
return "No words"
# remove punctuations
words = words.replace('.', '')
words = words.replace(',', '')
words = words.replace('!', '')
# strip
words=words.lstrip()
words=words.rstrip()
# split words by spaces
words = words.split(' ')
length = 0
# calculate total length of words
for word in words:
length += len(word)
# return average length
return length/len(words)
print(average_word_length("Hi"))
print(average_word_length("Hi, Lucy"))
print(average_word_length(" What big spaces you have!"))
print(average_word_length(True))
print(average_word_length("?!?!?! ... !"))
Screenshot of code:

Output:
