23. Working with Dictionaries and while Loops
By Bernd Klein. Last modified: 08 Nov 2023.
Introduction
In this chapter of our Python tutorial we assume that you are familiar with Python dictionaries and while
loops, as we have discussed them in our chapters Dictionaries and Loops.
We included this chapter to provide the learners with additional exercises of both dictionaries and while
loops. The focus lies on nested dictionaries, in our case dictionaries of dictionaries. From the experiences of my courses, I know that these often cause special difficulties in particular for beginners.
This chapter is also about coffee, tea and other hot drinks, whose consumption we manage using Python Dictionaries.
Live Python training
See our Python training courses
Coffee, Dictionary and a Loop
coffee_list = {"Peter": 0,
"Eva": 0,
"Franka": 0}
while True:
name = input("Name: ")
if name == "":
break
coffee_list[name] += 1
print(coffee_list[name])
print("coffee list: ", coffee_list)
OUTPUT:
1 1 2 1 coffee list: {'Peter': 2, 'Eva': 1, 'Franka': 1}
coffee_list = {"Peter": 0,
"Eva": 0,
"Franka": 0}
tea_list = {"Peter": 0,
"Eva": 0,
"Franka": 0}
while True:
name = input("Name: ")
if name == "":
break
getränk = input("Beverage (coffee/tea): ")
if getränk.lower() == "coffee":
coffee_list[name] += 1
print(coffee_list[name])
elif getränk.lower() == "tea":
tea_list[name] += 1
print(tea_list[name])
print("coffee list: ", coffee_list)
print("tea list: ", tea_list)
OUTPUT:
1 1 1 coffee list: {'Peter': 1, 'Eva': 0, 'Franka': 0} tea list: {'Peter': 0, 'Eva': 1, 'Franka': 1}
list_of_beverages = {"Peter": {"tea": 0,
"coffee": 0},
"Eva": {"tea": 0,
"coffee": 0},
"Franka": {"tea": 0,
"coffee": 0}}
while True:
name = input("Name: ").capitalize()
if name == "":
break
if name not in list_of_beverages:
antwort = input("Shall we include " + name + " in the list? (y/n)")
if antwort.lower() in ["y", "yes"]:
list_of_beverages[name] = {"tea": 0, "coffee": 0}
else:
print(name + " cannot have a drink!")
continue
drink = input("Beverage (coffee/tea): ")
list_of_beverages[name][drink] += 1
print(list_of_beverages)
OUTPUT:
Eve cannot have a drink! {'Peter': {'tea': 0, 'coffee': 1}, 'Eva': {'tea': 1, 'coffee': 0}, 'Franka': {'tea': 1, 'coffee': 0}}
supermarket = {"milk": {"quantity": 20, "price": 1.19},
"biscuits": {"quantity": 32, "price": 1.45},
"butter": {"quantity": 20, "price": 2.29},
"cheese": {"quantity": 15, "price": 1.90},
"bread": {"quantity": 15, "price": 2.59},
"cookies": {"quantity": 20, "price": 4.99},
"yogurt": {"quantity": 18, "price": 3.65},
"apples": {"quantity": 35, "price": 3.15},
"oranges": {"quantity": 40, "price": 0.99},
"bananas": {"quantity": 23, "price": 1.29}}
total_value = 0
for article, numbers in supermarket.items():
quantity = numbers["quantity"]
price = numbers["price"]
product_price = quantity * price
article = article + ':'
print(f"{article:15s} {product_price:08.2f}")
total_value += product_price
print("="*24)
print(f"Total sum: {total_value:08.2f}")
OUTPUT:
milk: 00023.80 biscuits: 00046.40 butter: 00045.80 cheese: 00028.50 bread: 00038.85 cookies: 00099.80 yogurt: 00065.70 apples: 00110.25 oranges: 00039.60 bananas: 00029.67 ======================== Total sum: 00528.37
Live Python training
Upcoming online Courses
31 Mar to 04 Apr 2025
07 Apr to 11 Apr 2025
19 May to 23 May 2025
02 Jun to 06 Jun 2025
30 Jun to 04 Jul 2025
11 Aug to 15 Aug 2025
10 Mar to 14 Mar 2025
07 Apr to 11 Apr 2025
23 Jun to 27 Jun 2025
28 Jul to 01 Aug 2025
Efficient Data Analysis with Pandas
10 Mar to 11 Mar 2025
07 Apr to 08 Apr 2025
02 Jun to 03 Jun 2025
23 Jun to 24 Jun 2025
28 Jul to 29 Jul 2025
Machine Learning from Data Preparation to Deep Learning
10 Mar to 14 Mar 2025
07 Apr to 11 Apr 2025
02 Jun to 06 Jun 2025
28 Jul to 01 Aug 2025
See our Python training courses