118k views
4 votes
The function below takes one parameter: a list of strings (string_list). Complete the function to compute the total combined length of all of the strings in the list The recommended approach for this: (1) create a variable to hold the current sum and initialize it to zero, (2) use a for loop to process each element of the list, adding its length to your current sum, (3) return the sum at the end.

User Antonin
by
5.4k points

1 Answer

1 vote

Answer:

def total_lengths(string_list):

total = 0

for s in string_list:

l = len(s)

total = total + l

return total

a_list = ["black", "blue", "white"]

print(total_lengths(a_list))

Step-by-step explanation:

Inside the function:

- Initialize total to zero to hold the sum

- In the for loop, assign the length of the each string to the l and add these values to the total

Then

- Create a list to check the function

- Call the function, pass the list as a parameter and print the result

User Deinumite
by
5.1k points