Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Take Dictionary From The Keyboard And Print The Sum Of Values
Title :
Write A Python Program To Take Dictionary From The Keyboard And Print The Sum Of Values
Description :
Hi guys, in this article you will learn About a Python program that takes a dictionary as input from the user and tries to calculate the sum of all the values present in the dictionary.
Let's get started with the program, take a length variable to store the number of dictionary items that the user will enter and use input function call to accept the dictionary length.
Then take another variable named as my_dictionary and initialize it to an Empty dictionary.
Now use the for loop to iterate from 0 to the dictionary length and keep asking a user to enter the dictionary key and value pairs. Here I'll be taking key and value pairs as both integer values.
Once you get the Key and value pair added to a dictionary
length = int(input("Enter Number of Dictionary Items : "))
my_dictionary = dict()
for _ in range(length):
key = int(input("Enter Dictionary Key : "))
value = int(input("Enter Dictionary Value : "))
my_dictionary[key] = value
Take another integer variable named the total and initialized to zero this variable will be used to store the sum of all the values present in the dictionary. now use the for loop to iterate all the values of the dictionary and add them to the total variable. Now finally using the print function print the sum of the value of the dictionary that is print the total variable.
total = 0
for item in my_dictionary.values():
total += item
print(f"Sum of Values of Dictionary : {total}")
Complete Python Code :
length = int(input("Enter Number of Dictionary Items : "))
my_dictionary = dict()
for _ in range(length):
key = int(input("Enter Dictionary Key : "))
value = int(input("Enter Dictionary Value : "))
my_dictionary[key] = value
total = 0
for item in my_dictionary.values():
total += item
print(f"Sum of Values of Dictionary : {total}")
Output :
Enter Number of Dictionary Items : 5
Enter Dictionary Key : 1
Enter Dictionary Value : 3
Enter Dictionary Key : 4
Enter Dictionary Value : 5
Enter Dictionary Key : 6
Enter Dictionary Value : 23
Enter Dictionary Key : 7
Enter Dictionary Value : 8
Enter Dictionary Key : 9
Enter Dictionary Value : 10
Sum of Values of Dictionary : 49
Output :
Enter Number of Dictionary Items : 3
Enter Dictionary Key : 1
Enter Dictionary Value : 1
Enter Dictionary Key : 2
Enter Dictionary Value : 2
Enter Dictionary Key : 3
Enter Dictionary Value : 3
Sum of Values of Dictionary : 6
Conclusion :
The above Python program Takes an Empty dictionary and keeps asking a user to enter the key and value using the for loop. Then similarly it uses the for loop to iterate all the items present in the dictionary and calculate sum of all the values present in the dictionary and finally print it. Comment it down below if you have any query is regarding the above Python program Subscribe to my YouTube channel, so you don't miss out new content
https://www.youtube.com/channel/UCUooZAqgfE4ngJ0oXsj3-MA
0 Comments