Search⌘ K

Solution: Create Two Dictionaries and Compute the Total Bill

Explore how to create two dictionaries for grocery items and quantities, then use a loop to calculate the total bill by computing the product of prices and quantities for each item. This lesson helps you apply dictionary operations and iteration in Python to solve practical problems.

We'll cover the following...

The solution to the problem of creating two dictionaries and computing the total bill using the values from the dictionaries is given below.

Solution

Python 3.8
# Calculate total bill amount
prices = { 'Bottles' : 30, 'Tiffin' : 100, 'Bag' : 400, 'Bicycle' : 2000 }
stock = { 'Bottles' : 10 , 'Tiffin' : 8, 'Bag' : 1, 'Bicycle' : 5}
total = 0
for key in prices :
value = prices[key] * stock[key]
total += value
print(key,value)
print('Total Bill Amount =' , total)
...