Skip to content

Commit

Permalink
add task for dict chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Feb 11, 2024
1 parent 9db6a1e commit c1d457e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion first_steps/bill.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Receipts

.. image:: receipts.jpg

Photo by `@carlijeen on unsplash.com <https://unsplash.com/@carlijeen?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Carli Jeen</a> on <a href="https://unsplash.com/photos/black-ceramic-cup-with-saucer-and-cappuccino-on-brown-wooden-surface-UWRqlJcDCXA?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash>`__
Photo by `Carli Jeen on unsplash.com <https://unsplash.com/@carlijeen?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash>`__


In this chapter you will:
Expand Down
6 changes: 3 additions & 3 deletions first_steps/cypher.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cesar Cypher
Cesar Cipher
============

.. image:: enigma.jpg
Expand All @@ -21,10 +21,10 @@ area topic
======= ====================================


Exercise 1: Cesar Cypher
Exercise 1: Cesar Cipher
------------------------

Execute the following code calculating a **Cesar Cypher**:
Execute the following code calculating a **Cesar Cipher**:

.. code:: python3
Expand Down
81 changes: 81 additions & 0 deletions first_steps/dictionaries.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Look up Prices
==============

In this chapter you will:
-------------------------

======= ====================================
area topic
======= ====================================
🚀 write a better receipt assistant
⚙ define a dictionary
⚙ look up values of a dictionary
🔀 iterate through a list of dictionary keys
🐞 fix index errors
======= ====================================

Exercise 1: Look up
-------------------

Consider we would like to look up shopping prices.
In this chapter, we will use a **dictionary**,
a data structure that is good for looking up items.

Execute the following code:

.. code:: python3
prices = {
"apple": 0.50,
"banana": 1.00,
"orange": 1.50,
"cherries": 3.00,
}
print(prices["banana"])
How does the dictionary differ from a list?


Exercise 2: Explore
-------------------

Find out what each of the expressions does to the dictionary in the center.

.. figure:: dicts.png
:alt: dict exercise


Exercise 3: Look up
-------------------

Now consider having the list of items in ``fruit``.
You would like to calculate the total price.
For that, the list and dictionary need to work together.

Sort the lines and indent them properly:

print(total)
total += prices[item]
bought = ["banana", "banana", "cherries", "apple", "apple", "banana"]
for item in bought:
total = 0
prices = {
"apple": 0.50,
"banana": 1.00,
"orange": 1.50,
"cherries": 3.00,
}

Exercise 4: Receipt assitant 2.0
--------------------------------

Improve the receipt assistant from the previous chapter
so that it uses a dictionary of prices.


Reflection Questions
--------------------

- How can you create a dictionary?
- How can you modify values in a dictionary?
- Is it possible to run a for loop over a dictionary?
File renamed without changes
2 changes: 1 addition & 1 deletion index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ First Steps
first_steps/cypher.rst
first_steps/bill.rst
first_steps/slideshow.rst
data_structures/dictionaries.md
first_steps/dictionaries.md


Tabular Data
Expand Down

0 comments on commit c1d457e

Please sign in to comment.