82.6k views
5 votes
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.

1 Answer

1 vote

Answer:

def average_strings(lst):

total_length = 0

avg = 0

for s in lst:

total_length += len(s)

avg = total_length/len(lst)

return avg

Step-by-step explanation:

Create a function called average_strings that takes one parameter, lst

Initialize the total length and avg variables

Create a for loop that iterates through the lst. Get each string's length and put it to the total length

When the loop is done, calculate the average, divide total length by length of the lst

Return the average

User Matthew MacGregor
by
7.0k points