How to Sum All the Values in a Dictionary.
There are two options while writing this program.
In this article, we will be looking at both options.
Option 1
Step 1: Define a Dictionary
Value1, Value2, Value3, Value 4 are our keys
10,20,30,40 are our values
my_dictionary1 = {'Value1': 10, 'Value2': 20, 'Value3': 30, 'Value4': 40}
Step 2: Write a statement to get the sum of the values from the dictionary you defined
sumtotal = sum(my_dictionary1.values())
Step 3: Print out the sum of your dictionary values along with a statement
print('The sum of all the values of your dictionary is: ', sumtotal)
Option 2
Step 1: Create a variable for your dictionary, then equate it to the dict function which creates a dictionary in your code
my_dictionary = dict()
Step 2: Introduce the variable run and set it equal to True for your while loop. Then introduce the variable count and equate it to 0. This will be useful in a later step.
run = True
count = 0
Step 3: Introduce yourself to the user with a print message, and then introduce the variable start_message and ask the user how many values he would like his dictionary to have.
In this option, your user can make his own dictionary.
print("Hello and welcome to this Python Program")
start_message = (int(input('How many values would you like your dictionary to have? Enter a whole number: ')))
Step 4: Start the while loop so that you can continue asking your user for the key and value multiple times.
while run:
Step 5: Now we must ask the user to input a key and value for his dictionary. Note that we must use the try and except tools for when we ask the user to input a value. This is because the user should only be allowed to enter numerical values for us to add. Then at the end we add a continue statement to ensure that the user is given another chance to enter a numerical key.
key = input('Enter the key: ')
try:
value = (int(input('Enter the numerical value: ')))
except ValueError:
print('Invalid Input. Please enter a number')
continue
Step 6: Now we come back to the variable we introduced in Step 2. This ensures that everytime the user adds a new key and value to the dictionary, we add one number. This ensures that the user is only prompted enough times so as to reach the number he inputted in Step 1.
count = count+1
Step 8: Here we will equate my dictionary variable's key to its value. This means that the key is = to the value
my_dictionary[key] = value
Step 7: Now we must write the function to get the sum of all the values
sumofvalues = sum(my_dictionary.values())
Step 8: This brings us back to Steps 2 & 6. Here we state that if the variable count is equal to the number the user inputted in Step 1, the loop must break or stop. We do not need to add an else statement
if count == start_message:
break
Step 9: This is the step where we print out the sumtotal that we calculated in Step 7. We also print out a message so as to give it a nice touch
print('The sum of all the values of your dictionary is: ', sumofvalues)
Limitations: This project would only add numerical values, it would not join string values in a dictionary.
Final Code:
Option 1:
#How to sum all the values in a dictionary
#Define a dictionary
my_dictionary1 = {'Value1': 10, 'Value2': 20, 'Value3': 30, 'Value4': 40}
sumtotal = sum(my_dictionary1.values())
print('The sum of all the values of your dictionary is: ', sumtotal)
Option 2:
#How to sum all the values in a dictionary #Define a dictionary my_dictionary = dict() run = True count = 0 print("Hello and welcome to this Python Program") start_message = (int(input('How many values would you like your dictionary to have? Enter a whole number: '))) while run: key = input('Enter the key: ') try: value = (int(input('Enter the numerical value: '))) except ValueError: print('Invalid Input. Please enter a number') continue count = count+1 my_dictionary[key] = value sumtotal = sum(my_dictionary.values()) if count == start_message: break else: pass print('The sum of all the values of your dictionary is: ', sumtotal)
Comments