Sum Values || Python

Sum Values || Python

Sum Values

For the first code challenge, we are going to look at only the values in a dictionary. This function should sum up all of the values from the key-value pairs in the dictionary. Here are the steps we need:

  1. Define the function to accept one parameter for our dictionary
  2. Create a variable to keep track of our sum
  3. Loop through every value in the dictionary
  4. Inside the loop, add each value to the sum
  5. Return the sum

Write a function named sum_values that takes a dictionary named my_dictionary as a parameter. The function should return the sum of the values of the dictionary

No alt text provided for this image

We start by creating a variable to keep track of the total. Next, we use the values() function in our for loop in order to iterate through each of the values in the dictionary. Using this, we can access each value and add it to our total variable. At the end of our loop, we return the total.

To view or add a comment, sign in

More articles by Ahmed Khaleel

Insights from the community

Others also viewed

Explore topics