Define a function below called average_strings. The function takes one argument: a list of strings. Complete the function so that it returns the average length of the strings in the list. An empty list should have an average of zero. Hint: don't forget that in order to get a string's length, you need the len function.

Respuesta :

Answer:

def average_strings(lst):

   total = 0

   if len(lst) == 0:

       return 0

   for s in lst:

       total += len(s)

   return total / len(lst)

Explanation:

Create a function called average_strings that take one parameter, lst

Initialize a total to hold the length of the strings in lst

If the length of lst is 0, return 0

Otherwise create a for loop that iterates through the lst. Calculate the length of each string in lst and add it to total

When the loop is done, return the average, total length of the strings divided by length of the lst

fichoh

The required code written in python 3 which returns the average of the strings in a list goes thus :

def average_strings(string_list):

#initialize a function named average_string which takes in a list of string as parameter

total_length = 0

#initialize total length of string to 0

if len(string_list)==0:

#checks of the string is empty

return 0

#if string is empty return 0

for word in string_list :

total_length += len(word)

#add the length of each string to the total length of the string.

return total_length / len(string_list)

#return the average value

A sample run of the function is given here and the output attached.

string_list = ['dog', 'cat', 'mouse']

print(average_strings(string_list))

Learn more :https://brainly.com/question/15352352

Ver imagen fichoh