9.7k views
3 votes
Define a function below, sum_numeric_vals, which takes a single dictionary as a parameter. The dictionary has only strings for keys, but can have string or integer values. Complete the function to calculate and return the sum of the integer values in the dictionary.

User Lavar
by
5.6k points

1 Answer

2 votes

Answer:

Following are the program in the Python Programming language.

#define function

def sum_numeric_vals(dic):

#set and initialize the integer variable to 0

sum_total = 0

#set the for loop to extract the value of dictionary

for val in dic.values():

#set if condition to find the sum

if type(val)==int:

sum_total += val

#return the sum of the values

return sum_total

#set and initialize dictionary type variable

dic={"Two":2,"Four":4}

#call and print the function

print(sum_numeric_vals(dic))

Output:

6

Step-by-step explanation:

Here, we define the function "sum_numeric_vals()" and pass an argument "dic", inside the function.

  • Set and initialize an integer data type variable "sum_total" to 0.
  • Set the for loop which extract the values of the dictionary.
  • Set the if conditional statement inside the loop to find the sum of the values of the dictionary.
  • Then, return the sum of the values of the dictionary.

Finally, we set and initialize the dictionary type variable "dic" and pass it in the function's argument list then, call the function through the print function

User Mitja
by
5.3k points