[ Web Proxy ]
URL:
Viewing: https://python-course.eu/python-tutorial/working-with-dictionaries-and-while-loops.php [Back]  [Original]

23. Working with Dictionaries and while Loops | Python Tutorial
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

pycoffee [pycoffee]

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

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.

Coffee Vending Machine [Coffee Vending Machine]

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with 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

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page